From 37ee00077a6b404534181988c0630bbdf98ca2ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Sat, 29 Jun 2019 00:27:20 +0200 Subject: [PATCH] Implement edit bookmark route --- .idea/dataSources.xml | 2 +- bookmarks/migrations/0001_initial.py | 6 ++++-- bookmarks/models.py | 6 ++++-- bookmarks/services/bookmarks.py | 9 +++++++++ bookmarks/templates/bookmarks/edit.html | 21 ++++++++++++++++++++- bookmarks/templates/bookmarks/new.html | 2 +- bookmarks/views.py | 17 ++++++++++++----- 7 files changed, 51 insertions(+), 12 deletions(-) diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 194c065..507b0ad 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,7 +1,7 @@ - + sqlite.xerial true org.sqlite.JDBC diff --git a/bookmarks/migrations/0001_initial.py b/bookmarks/migrations/0001_initial.py index 91db388..64074e8 100644 --- a/bookmarks/migrations/0001_initial.py +++ b/bookmarks/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.2 on 2019-06-28 17:20 +# Generated by Django 2.2.2 on 2019-06-28 22:16 from django.conf import settings from django.db import migrations, models @@ -18,13 +18,15 @@ class Migration(migrations.Migration): name='Bookmark', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('url', models.TextField()), + ('url', models.URLField()), ('title', models.CharField(max_length=512)), ('description', models.TextField()), ('website_title', models.CharField(max_length=512)), ('website_description', models.TextField()), ('unread', models.BooleanField(default=True)), ('date_added', models.DateTimeField()), + ('date_modified', models.DateTimeField()), + ('date_accessed', models.DateTimeField(blank=True, null=True)), ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ], ), diff --git a/bookmarks/models.py b/bookmarks/models.py index febde4a..ea2c7ff 100644 --- a/bookmarks/models.py +++ b/bookmarks/models.py @@ -4,13 +4,15 @@ from django.db import models # Create your models here. class Bookmark(models.Model): - url = models.TextField() + url = models.URLField() title = models.CharField(max_length=512) description = models.TextField() website_title = models.CharField(max_length=512) website_description = models.TextField() unread = models.BooleanField(default=True) date_added = models.DateTimeField() + date_modified = models.DateTimeField() + date_accessed = models.DateTimeField(blank=True, null=True) owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) @property @@ -22,4 +24,4 @@ class Bookmark(models.Model): return self.website_description if not self.description else self.description def __str__(self): - return self.title + ' (' + self.url[:30] + '...)' + return self.resolved_title + ' (' + self.url[:30] + '...)' diff --git a/bookmarks/services/bookmarks.py b/bookmarks/services/bookmarks.py index bb6c0c4..df40ac7 100644 --- a/bookmarks/services/bookmarks.py +++ b/bookmarks/services/bookmarks.py @@ -11,6 +11,15 @@ def create_bookmark(bookmark: Bookmark, current_user: User): bookmark.owner = current_user # Set dates bookmark.date_added = timezone.now() + bookmark.date_modified = timezone.now() + bookmark.save() + + +def update_bookmark(bookmark: Bookmark): + # Update website info + _update_website_metadata(bookmark) + # Update dates + bookmark.date_modified = timezone.now() bookmark.save() diff --git a/bookmarks/templates/bookmarks/edit.html b/bookmarks/templates/bookmarks/edit.html index 7de25ad..aca5889 100644 --- a/bookmarks/templates/bookmarks/edit.html +++ b/bookmarks/templates/bookmarks/edit.html @@ -1,5 +1,24 @@ {% extends "bookmarks/layout.html" %} {% block content %} -

Edit bookmark

+

New bookmark

+
+ {% csrf_token %} +
+ + {{ form.url }} + {{ form.url.errors }} +
+
+ + {{ form.title }} + {{ form.title.errors }} +
+
+ + {{ form.description }} + {{ form.description.errors }} +
+ +
{% endblock %} diff --git a/bookmarks/templates/bookmarks/new.html b/bookmarks/templates/bookmarks/new.html index 973258a..d9f0ae2 100644 --- a/bookmarks/templates/bookmarks/new.html +++ b/bookmarks/templates/bookmarks/new.html @@ -19,6 +19,6 @@ {{ form.description }} {{ form.description.errors }} - + {% endblock %} diff --git a/bookmarks/views.py b/bookmarks/views.py index 20dae94..b77091d 100644 --- a/bookmarks/views.py +++ b/bookmarks/views.py @@ -2,7 +2,7 @@ from django.http import HttpResponseRedirect, HttpRequest from django.shortcuts import render from django.urls import reverse -from bookmarks.services.bookmarks import create_bookmark +from bookmarks.services.bookmarks import create_bookmark, update_bookmark from . import forms from .models import Bookmark @@ -29,10 +29,17 @@ def new(request: HttpRequest): def edit(request, bookmark_id): - context = { - 'bookmark': Bookmark.objects.get(pk=bookmark_id) - } - return render(request, 'bookmarks/edit.html', context) + bookmark = Bookmark.objects.get(pk=bookmark_id) + if request.method == 'POST': + form = forms.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) + + return render(request, 'bookmarks/edit.html', {'form': form, 'bookmark_id': bookmark_id}) def remove(request, bookmark_id: int):