From ba3d4eb663076f02803e8512c969d050647047be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Sat, 29 Jun 2019 01:08:22 +0200 Subject: [PATCH] Refactor packages --- bookmarks/forms/__init__.py | 1 - bookmarks/forms/bookmark_form.py | 20 -------------------- bookmarks/models.py | 20 +++++++++++++++++++- bookmarks/templatetags/bookmarks.py | 2 +- bookmarks/urls.py | 2 +- bookmarks/views/__init__.py | 1 + bookmarks/{views.py => views/bookmarks.py} | 11 +++++------ 7 files changed, 27 insertions(+), 30 deletions(-) delete mode 100644 bookmarks/forms/__init__.py delete mode 100644 bookmarks/forms/bookmark_form.py create mode 100644 bookmarks/views/__init__.py rename bookmarks/{views.py => views/bookmarks.py} (83%) diff --git a/bookmarks/forms/__init__.py b/bookmarks/forms/__init__.py deleted file mode 100644 index 46a0f9f..0000000 --- a/bookmarks/forms/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .bookmark_form import * diff --git a/bookmarks/forms/bookmark_form.py b/bookmarks/forms/bookmark_form.py deleted file mode 100644 index f7ce222..0000000 --- a/bookmarks/forms/bookmark_form.py +++ /dev/null @@ -1,20 +0,0 @@ -from django import forms - -from bookmarks.models import Bookmark - -auto_fill_placeholder = 'Leave empty to fill from website metadata' - - -class BookmarkForm(forms.ModelForm): - # Use URLField for URL - url = forms.URLField() - # Do not require title and description in form as we fill these automatically if they are empty - title = forms.CharField(max_length=512, - required=False, - widget=forms.TextInput(attrs={'placeholder': auto_fill_placeholder})) - description = forms.CharField(required=False, - widget=forms.Textarea(attrs={'placeholder': auto_fill_placeholder})) - - class Meta: - model = Bookmark - fields = ['url', 'title', 'description'] diff --git a/bookmarks/models.py b/bookmarks/models.py index ea2c7ff..ecca4b4 100644 --- a/bookmarks/models.py +++ b/bookmarks/models.py @@ -1,8 +1,8 @@ +from django import forms from django.contrib.auth import get_user_model from django.db import models -# Create your models here. class Bookmark(models.Model): url = models.URLField() title = models.CharField(max_length=512) @@ -25,3 +25,21 @@ class Bookmark(models.Model): def __str__(self): return self.resolved_title + ' (' + self.url[:30] + '...)' + + +auto_fill_placeholder = 'Leave empty to fill from website metadata' + + +class BookmarkForm(forms.ModelForm): + # Use URLField for URL + url = forms.URLField() + # Do not require title and description in form as we fill these automatically if they are empty + title = forms.CharField(max_length=512, + required=False, + widget=forms.TextInput(attrs={'placeholder': auto_fill_placeholder})) + description = forms.CharField(required=False, + widget=forms.Textarea(attrs={'placeholder': auto_fill_placeholder})) + + class Meta: + model = Bookmark + fields = ['url', 'title', 'description'] diff --git a/bookmarks/templatetags/bookmarks.py b/bookmarks/templatetags/bookmarks.py index 51e2c15..2b2b96d 100644 --- a/bookmarks/templatetags/bookmarks.py +++ b/bookmarks/templatetags/bookmarks.py @@ -1,6 +1,6 @@ from django import template -from bookmarks.forms import BookmarkForm +from bookmarks.models import BookmarkForm register = template.Library() diff --git a/bookmarks/urls.py b/bookmarks/urls.py index d913a7b..8d2d334 100644 --- a/bookmarks/urls.py +++ b/bookmarks/urls.py @@ -8,9 +8,9 @@ app_name = 'bookmarks' urlpatterns = [ # Redirect root to bookmarks index url(r'^$', RedirectView.as_view(pattern_name='bookmarks:index', permanent=False)), + # Bookmarks path('bookmarks', views.index, name='index'), path('bookmarks/new', views.new, name='new'), path('bookmarks//edit', views.edit, name='edit'), - # path('bookmarks//update', views.update, name='edit'), path('bookmarks//remove', views.remove, name='remove'), ] diff --git a/bookmarks/views/__init__.py b/bookmarks/views/__init__.py new file mode 100644 index 0000000..37f2e1d --- /dev/null +++ b/bookmarks/views/__init__.py @@ -0,0 +1 @@ +from .bookmarks import * diff --git a/bookmarks/views.py b/bookmarks/views/bookmarks.py similarity index 83% rename from bookmarks/views.py rename to bookmarks/views/bookmarks.py index b77091d..9571d8c 100644 --- a/bookmarks/views.py +++ b/bookmarks/views/bookmarks.py @@ -2,9 +2,8 @@ from django.http import HttpResponseRedirect, HttpRequest from django.shortcuts import render from django.urls import reverse +from bookmarks.models import Bookmark, BookmarkForm from bookmarks.services.bookmarks import create_bookmark, update_bookmark -from . import forms -from .models import Bookmark def index(request): @@ -16,14 +15,14 @@ def index(request): def new(request: HttpRequest): if request.method == 'POST': - form = forms.BookmarkForm(request.POST) + form = BookmarkForm(request.POST) if form.is_valid(): bookmark = form.save(commit=False) current_user = request.user create_bookmark(bookmark, current_user) return HttpResponseRedirect(reverse('bookmarks:index')) else: - form = forms.BookmarkForm() + form = BookmarkForm() return render(request, 'bookmarks/new.html', {'form': form}) @@ -31,13 +30,13 @@ def new(request: HttpRequest): def edit(request, bookmark_id): bookmark = Bookmark.objects.get(pk=bookmark_id) if request.method == 'POST': - form = forms.BookmarkForm(request.POST, instance=bookmark) + form = BookmarkForm(request.POST, instance=bookmark) if form.is_valid(): bookmark = form.save(commit=False) update_bookmark(bookmark) return HttpResponseRedirect(reverse('bookmarks:index')) else: - form = forms.BookmarkForm(instance=bookmark) + form = BookmarkForm(instance=bookmark) return render(request, 'bookmarks/edit.html', {'form': form, 'bookmark_id': bookmark_id})