Implement add bookmark route

This commit is contained in:
Sascha Ißbrücker 2019-06-28 19:37:41 +02:00
parent e2a834a56c
commit c653206dd3
11 changed files with 100 additions and 13 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.sqlite3" uuid="3828bbfa-0339-4f6b-adbf-4e1b50aa80f6">
<data-source source="LOCAL" name="db" uuid="06992f5d-20e5-4663-9238-0995a41804e3">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
@ -9,6 +9,14 @@
<driver-properties>
<property name="enable_load_extension" value="true" />
</driver-properties>
<libraries>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.25.1/sqlite-jdbc-3.25.1.jar</url>
</library>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.25.1/license.txt</url>
</library>
</libraries>
</data-source>
</component>
</project>

View File

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

View File

@ -0,0 +1,20 @@
from django import forms
from ..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,4 +1,4 @@
# Generated by Django 2.2.2 on 2019-06-26 11:39
# Generated by Django 2.2.2 on 2019-06-28 17:20
from django.conf import settings
from django.db import migrations, models
@ -21,6 +21,8 @@ class Migration(migrations.Migration):
('url', models.TextField()),
('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()),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),

View File

@ -7,9 +7,19 @@ class Bookmark(models.Model):
url = models.TextField()
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()
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
@property
def resolved_title(self):
return self.website_title if not self.title else self.title
@property
def resolved_description(self):
return self.website_description if not self.description else self.description
def __str__(self):
return self.title + ' (' + self.url[:30] + '...)'

View File

View File

@ -0,0 +1,21 @@
from django.contrib.auth.models import User
from django.utils import timezone
from bookmarks.models import Bookmark
def create_bookmark(bookmark: Bookmark, current_user: User):
# Update website info
_update_website_metadata(bookmark)
# Set currently logged in user as owner
bookmark.owner = current_user
# Set dates
bookmark.date_added = timezone.now()
bookmark.save()
def _update_website_metadata(bookmark: Bookmark):
# TODO: Load website metadata
bookmark.website_title = 'Title from website'
bookmark.website_description = 'Description from website'
pass

View File

@ -9,10 +9,10 @@
{% for bookmark in bookmarks %}
<li>
<p>
<a href="{{ bookmark.url }}" target="_blank">{{ bookmark.title }}</a>
<a href="{{ bookmark.url }}" target="_blank">{{ bookmark.resolved_title }}</a>
</p>
{% if bookmark.description is not None %}
<p>{{ bookmark.description }}</p>
{% if bookmark.resolved_description is not None %}
<p>{{ bookmark.resolved_description }}</p>
{% endif %}
<p>
<a href="{% url 'bookmarks:edit' bookmark.id %}">Edit</a>

View File

@ -2,4 +2,23 @@
{% block content %}
<h2>New bookmark</h2>
<form action="{% url 'bookmarks:new' %}" 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="Add">
</form>
{% endblock %}

View File

@ -10,7 +10,6 @@ urlpatterns = [
url(r'^$', RedirectView.as_view(pattern_name='bookmarks:index', permanent=False)),
path('bookmarks', views.index, name='index'),
path('bookmarks/new', views.new, name='new'),
# path('bookmarks/create', views.create, name='create'),
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'),

View File

@ -1,8 +1,9 @@
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponseRedirect, HttpRequest
from django.shortcuts import render
# Create your views here.
from django.urls import reverse
from bookmarks.services.bookmarks import create_bookmark
from . import forms
from .models import Bookmark
@ -13,12 +14,18 @@ def index(request):
return render(request, 'bookmarks/index.html', context)
def create(request):
return HttpResponse('OK')
def new(request: HttpRequest):
if request.method == 'POST':
form = forms.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()
def new(request):
return render(request, 'bookmarks/new.html')
return render(request, 'bookmarks/new.html', {'form': form})
def edit(request, bookmark_id):