diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index c61958a..194c065 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,7 +1,7 @@ - + sqlite.xerial true org.sqlite.JDBC @@ -9,6 +9,14 @@ + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.25.1/sqlite-jdbc-3.25.1.jar + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.25.1/license.txt + + \ No newline at end of file diff --git a/bookmarks/forms/__init__.py b/bookmarks/forms/__init__.py new file mode 100644 index 0000000..46a0f9f --- /dev/null +++ b/bookmarks/forms/__init__.py @@ -0,0 +1 @@ +from .bookmark_form import * diff --git a/bookmarks/forms/bookmark_form.py b/bookmarks/forms/bookmark_form.py new file mode 100644 index 0000000..f5e96fa --- /dev/null +++ b/bookmarks/forms/bookmark_form.py @@ -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'] diff --git a/bookmarks/migrations/0001_initial.py b/bookmarks/migrations/0001_initial.py index 5036fdc..91db388 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-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)), diff --git a/bookmarks/models.py b/bookmarks/models.py index fc0b84b..febde4a 100644 --- a/bookmarks/models.py +++ b/bookmarks/models.py @@ -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] + '...)' diff --git a/bookmarks/services/__init__.py b/bookmarks/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bookmarks/services/bookmarks.py b/bookmarks/services/bookmarks.py new file mode 100644 index 0000000..bb6c0c4 --- /dev/null +++ b/bookmarks/services/bookmarks.py @@ -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 diff --git a/bookmarks/templates/bookmarks/index.html b/bookmarks/templates/bookmarks/index.html index 4774953..7f3cf69 100644 --- a/bookmarks/templates/bookmarks/index.html +++ b/bookmarks/templates/bookmarks/index.html @@ -9,10 +9,10 @@ {% for bookmark in bookmarks %}
  • - {{ bookmark.title }} + {{ bookmark.resolved_title }}

    - {% if bookmark.description is not None %} -

    {{ bookmark.description }}

    + {% if bookmark.resolved_description is not None %} +

    {{ bookmark.resolved_description }}

    {% endif %}

    Edit diff --git a/bookmarks/templates/bookmarks/new.html b/bookmarks/templates/bookmarks/new.html index 87bec1b..973258a 100644 --- a/bookmarks/templates/bookmarks/new.html +++ b/bookmarks/templates/bookmarks/new.html @@ -2,4 +2,23 @@ {% block content %}

    New bookmark

    +
    + {% csrf_token %} +
    + + {{ form.url }} + {{ form.url.errors }} +
    +
    + + {{ form.title }} + {{ form.title.errors }} +
    +
    + + {{ form.description }} + {{ form.description.errors }} +
    + +
    {% endblock %} diff --git a/bookmarks/urls.py b/bookmarks/urls.py index 7ed5ce2..d913a7b 100644 --- a/bookmarks/urls.py +++ b/bookmarks/urls.py @@ -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//edit', views.edit, name='edit'), # path('bookmarks//update', views.update, name='edit'), path('bookmarks//remove', views.remove, name='remove'), diff --git a/bookmarks/views.py b/bookmarks/views.py index ad18152..20dae94 100644 --- a/bookmarks/views.py +++ b/bookmarks/views.py @@ -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):