Implement edit bookmark route

This commit is contained in:
Sascha Ißbrücker 2019-06-29 00:27:20 +02:00
parent c653206dd3
commit 37ee00077a
7 changed files with 51 additions and 12 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="db" uuid="06992f5d-20e5-4663-9238-0995a41804e3">
<data-source source="LOCAL" name="db" uuid="b92a6ddc-c3eb-4f61-9408-7ee5d46a163b">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>

View File

@ -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)),
],
),

View File

@ -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] + '...)'

View File

@ -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()

View File

@ -1,5 +1,24 @@
{% extends "bookmarks/layout.html" %}
{% block content %}
<h2>Edit bookmark</h2>
<h2>New bookmark</h2>
<form action="{% url 'bookmarks:edit' bookmark_id %}" method="post">
{% csrf_token %}
<div class="field">
<label for="{{ form.url.id_for_label }}">URL</label>
{{ form.url }}
{{ form.url.errors }}
</div>
<div class="field">
<label for="{{ form.title.id_for_label }}">Title</label>
{{ form.title }}
{{ form.title.errors }}
</div>
<div class="field">
<label for="{{ form.description.id_for_label }}">Description</label>
{{ form.description }}
{{ form.description.errors }}
</div>
<input type="submit" value="Save">
</form>
{% endblock %}

View File

@ -19,6 +19,6 @@
{{ form.description }}
{{ form.description.errors }}
</div>
<input type="submit" value="Add">
<input type="submit" value="Save">
</form>
{% endblock %}

View File

@ -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):