2019-06-28 23:08:22 +00:00
|
|
|
from django import forms
|
2019-06-27 06:09:51 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
|
|
|
class Bookmark(models.Model):
|
2019-06-28 22:27:20 +00:00
|
|
|
url = models.URLField()
|
2019-06-27 06:09:51 +00:00
|
|
|
title = models.CharField(max_length=512)
|
|
|
|
description = models.TextField()
|
2019-06-28 17:37:41 +00:00
|
|
|
website_title = models.CharField(max_length=512)
|
|
|
|
website_description = models.TextField()
|
2019-06-27 06:09:51 +00:00
|
|
|
unread = models.BooleanField(default=True)
|
|
|
|
date_added = models.DateTimeField()
|
2019-06-28 22:27:20 +00:00
|
|
|
date_modified = models.DateTimeField()
|
|
|
|
date_accessed = models.DateTimeField(blank=True, null=True)
|
2019-06-27 06:09:51 +00:00
|
|
|
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
|
|
|
|
2019-06-28 17:37:41 +00:00
|
|
|
@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
|
|
|
|
|
2019-06-27 06:09:51 +00:00
|
|
|
def __str__(self):
|
2019-06-28 22:27:20 +00:00
|
|
|
return self.resolved_title + ' (' + self.url[:30] + '...)'
|
2019-06-28 23:08:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
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']
|