Refactor packages

This commit is contained in:
Sascha Ißbrücker 2019-06-29 01:08:22 +02:00
parent 251fda609c
commit ba3d4eb663
7 changed files with 27 additions and 30 deletions

View File

@ -1 +0,0 @@
from .bookmark_form import *

View File

@ -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']

View File

@ -1,8 +1,8 @@
from django import forms
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db import models from django.db import models
# Create your models here.
class Bookmark(models.Model): class Bookmark(models.Model):
url = models.URLField() url = models.URLField()
title = models.CharField(max_length=512) title = models.CharField(max_length=512)
@ -25,3 +25,21 @@ class Bookmark(models.Model):
def __str__(self): def __str__(self):
return self.resolved_title + ' (' + self.url[:30] + '...)' 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']

View File

@ -1,6 +1,6 @@
from django import template from django import template
from bookmarks.forms import BookmarkForm from bookmarks.models import BookmarkForm
register = template.Library() register = template.Library()

View File

@ -8,9 +8,9 @@ app_name = 'bookmarks'
urlpatterns = [ urlpatterns = [
# Redirect root to bookmarks index # Redirect root to bookmarks index
url(r'^$', RedirectView.as_view(pattern_name='bookmarks:index', permanent=False)), url(r'^$', RedirectView.as_view(pattern_name='bookmarks:index', permanent=False)),
# Bookmarks
path('bookmarks', views.index, name='index'), path('bookmarks', views.index, name='index'),
path('bookmarks/new', views.new, name='new'), path('bookmarks/new', views.new, name='new'),
path('bookmarks/<int:bookmark_id>/edit', views.edit, name='edit'), path('bookmarks/<int:bookmark_id>/edit', views.edit, name='edit'),
# path('bookmarks/<int:bookmark_id>/update', views.update, name='edit'),
path('bookmarks/<int:bookmark_id>/remove', views.remove, name='remove'), path('bookmarks/<int:bookmark_id>/remove', views.remove, name='remove'),
] ]

View File

@ -0,0 +1 @@
from .bookmarks import *

View File

@ -2,9 +2,8 @@ from django.http import HttpResponseRedirect, HttpRequest
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse from django.urls import reverse
from bookmarks.models import Bookmark, BookmarkForm
from bookmarks.services.bookmarks import create_bookmark, update_bookmark from bookmarks.services.bookmarks import create_bookmark, update_bookmark
from . import forms
from .models import Bookmark
def index(request): def index(request):
@ -16,14 +15,14 @@ def index(request):
def new(request: HttpRequest): def new(request: HttpRequest):
if request.method == 'POST': if request.method == 'POST':
form = forms.BookmarkForm(request.POST) form = BookmarkForm(request.POST)
if form.is_valid(): if form.is_valid():
bookmark = form.save(commit=False) bookmark = form.save(commit=False)
current_user = request.user current_user = request.user
create_bookmark(bookmark, current_user) create_bookmark(bookmark, current_user)
return HttpResponseRedirect(reverse('bookmarks:index')) return HttpResponseRedirect(reverse('bookmarks:index'))
else: else:
form = forms.BookmarkForm() form = BookmarkForm()
return render(request, 'bookmarks/new.html', {'form': form}) return render(request, 'bookmarks/new.html', {'form': form})
@ -31,13 +30,13 @@ def new(request: HttpRequest):
def edit(request, bookmark_id): def edit(request, bookmark_id):
bookmark = Bookmark.objects.get(pk=bookmark_id) bookmark = Bookmark.objects.get(pk=bookmark_id)
if request.method == 'POST': if request.method == 'POST':
form = forms.BookmarkForm(request.POST, instance=bookmark) form = BookmarkForm(request.POST, instance=bookmark)
if form.is_valid(): if form.is_valid():
bookmark = form.save(commit=False) bookmark = form.save(commit=False)
update_bookmark(bookmark) update_bookmark(bookmark)
return HttpResponseRedirect(reverse('bookmarks:index')) return HttpResponseRedirect(reverse('bookmarks:index'))
else: else:
form = forms.BookmarkForm(instance=bookmark) form = BookmarkForm(instance=bookmark)
return render(request, 'bookmarks/edit.html', {'form': form, 'bookmark_id': bookmark_id}) return render(request, 'bookmarks/edit.html', {'form': form, 'bookmark_id': bookmark_id})