From 553a756068220cf5ee3253df358f2a270c5d52d3 Mon Sep 17 00:00:00 2001 From: Tiberiu Ichim Date: Fri, 4 Jan 2019 21:36:31 +0200 Subject: Improve compatibility with third party apps --- weblog/models.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'weblog') diff --git a/weblog/models.py b/weblog/models.py index cccb8d3..a1f4aa5 100755 --- a/weblog/models.py +++ b/weblog/models.py @@ -1,8 +1,10 @@ -from django.db import models -from django.shortcuts import reverse +from django.conf import settings from django.contrib.auth.models import User +from django.db import models from django.forms import ModelForm, Textarea -from django.utils.translation import ugettext_lazy as _, pgettext_lazy +from django.shortcuts import reverse +from django.utils.translation import pgettext_lazy, ugettext_lazy as _ + class Category(models.Model): name = models.CharField(max_length=250, verbose_name=pgettext_lazy('Noun, not personal name', 'Name'), blank=False, unique=True) @@ -36,7 +38,7 @@ class CategoryTranslation(models.Model): class BlogPost(models.Model): - author = models.ForeignKey(User, verbose_name=_('Author'), on_delete=models.PROTECT) + author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('Author'), on_delete=models.PROTECT) title = models.CharField(max_length=100, verbose_name=pgettext_lazy('As in name', 'Title'), blank=False) content = models.TextField(verbose_name=pgettext_lazy('Of post, comment, article, etc.', 'Content'), blank=False) preview_image = models.ImageField(upload_to='weblog/preview_images/%Y/%m/%d/', blank=True, verbose_name=_('Preview image')) @@ -52,6 +54,7 @@ class BlogPost(models.Model): def get_absolute_url(self): if self.categories.all().count() > 0: category = self.categories.all()[0].slug + return reverse('weblog:PostView', kwargs={'category_slug': category, 'post_slug': self.slug}) else: return reverse('weblog:PostView', kwargs={'category_slug': 'misc', 'post_slug': self.slug}) @@ -77,7 +80,7 @@ class Translation(models.Model): verbose_name_plural = _('Translations') class PostComment(models.Model): - author = models.ForeignKey(User, verbose_name=_('Author'), null=True, blank=True, on_delete=models.PROTECT) + author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('Author'), null=True, blank=True, on_delete=models.PROTECT) post = models.ForeignKey(BlogPost, verbose_name=pgettext_lazy('Noun, as in blog post', 'Post'), on_delete=models.CASCADE) content = models.TextField(verbose_name=pgettext_lazy('Of post, comment, article, etc.', 'Content'), blank=False) publish_date = models.DateTimeField(verbose_name=_('Publish date')) -- cgit v1.2.3 From 0e0f98ed346f0d2a6fb8fbbec49090d183ba996f Mon Sep 17 00:00:00 2001 From: Tiberiu Ichim Date: Fri, 4 Jan 2019 21:38:43 +0200 Subject: Added merge migration --- weblog/migrations/0009_merge_20190104_2123.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 weblog/migrations/0009_merge_20190104_2123.py (limited to 'weblog') diff --git a/weblog/migrations/0009_merge_20190104_2123.py b/weblog/migrations/0009_merge_20190104_2123.py new file mode 100644 index 0000000..5c8ba71 --- /dev/null +++ b/weblog/migrations/0009_merge_20190104_2123.py @@ -0,0 +1,14 @@ +# Generated by Django 2.1.3 on 2019-01-04 19:23 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('weblog', '0008_auto_20180329_1316'), + ('weblog', '0008_auto_20180531_0329'), + ] + + operations = [ + ] -- cgit v1.2.3 From 0060b2ec28f00b59c0f0d3fa19a1715f1c7c6513 Mon Sep 17 00:00:00 2001 From: Tiberiu Ichim Date: Sat, 5 Jan 2019 12:24:24 +0100 Subject: Autopep8 some files --- weblog/admin.py | 8 +++ weblog/apps.py | 1 + weblog/models.py | 97 +++++++++++++++++++++++++----------- weblog/templatetags/weblog_extras.py | 49 +++++++++++++----- 4 files changed, 112 insertions(+), 43 deletions(-) (limited to 'weblog') diff --git a/weblog/admin.py b/weblog/admin.py index 1b4e128..aa5acfb 100755 --- a/weblog/admin.py +++ b/weblog/admin.py @@ -7,18 +7,22 @@ from .models import BlogPost, Translation, PostComment, Category, CategoryTransl blogPostInlines = [] categoryInlines = [] + class TranslationInline(admin.StackedInline, SummernoteInlineModelAdmin): model = Translation extra = 1 + class CategoryTranslationInline(admin.StackedInline): model = CategoryTranslation extra = 1 + class PostCommentInline(admin.StackedInline): model = PostComment extra = 0 + if blog_settings['multilingual']: blogPostInlines.append(TranslationInline) categoryInlines.append(CategoryTranslationInline) @@ -26,6 +30,7 @@ if blog_settings['multilingual']: if blog_settings['enable_comments']: blogPostInlines.append(PostCommentInline) + class BlogPostAdmin(SummernoteModelAdmin): list_display = ['title', 'author', 'publish_date'] list_filter = ['publish_date', 'categories'] @@ -36,11 +41,14 @@ class BlogPostAdmin(SummernoteModelAdmin): if not blog_settings['multilingual']: self.exclude = ('original_language', ) form = super(BlogPostAdmin, self).get_form(request, obj, **kwargs) + return form + class CategoryAdmin(admin.ModelAdmin): list_display = ['name'] inlines = categoryInlines + admin.site.register(BlogPost, BlogPostAdmin) admin.site.register(Category, CategoryAdmin) diff --git a/weblog/apps.py b/weblog/apps.py index 2c094ee..bcc5a15 100755 --- a/weblog/apps.py +++ b/weblog/apps.py @@ -18,5 +18,6 @@ SETTINGS = { 'enable_rss': True, } + class WeblogConfig(AppConfig): name = 'weblog' diff --git a/weblog/models.py b/weblog/models.py index a1f4aa5..1ccfbd0 100755 --- a/weblog/models.py +++ b/weblog/models.py @@ -1,5 +1,4 @@ from django.conf import settings -from django.contrib.auth.models import User from django.db import models from django.forms import ModelForm, Textarea from django.shortcuts import reverse @@ -7,12 +6,17 @@ from django.utils.translation import pgettext_lazy, ugettext_lazy as _ class Category(models.Model): - name = models.CharField(max_length=250, verbose_name=pgettext_lazy('Noun, not personal name', 'Name'), blank=False, unique=True) - slug = models.SlugField(max_length=60, verbose_name=_('Slug (URL)'), db_index=True, unique=True) - parent_category = models.ForeignKey('self', verbose_name=_('Parent category'), null=True, blank=True, default=None, on_delete=models.SET_DEFAULT) + name = models.CharField(max_length=250, verbose_name=pgettext_lazy( + 'Noun, not personal name', 'Name'), blank=False, unique=True) + slug = models.SlugField(max_length=60, verbose_name=_( + 'Slug (URL)'), db_index=True, unique=True) + parent_category = models.ForeignKey( + 'self', verbose_name=_('Parent category'), null=True, blank=True, + default=None, on_delete=models.SET_DEFAULT) def get_absolute_url(self): - return reverse('weblog:CategoryIndex', kwargs={'category_slug': self.slug}) + return reverse('weblog:CategoryIndex', + kwargs={'category_slug': self.slug}) def __str__(self): return self.name @@ -21,10 +25,14 @@ class Category(models.Model): verbose_name = pgettext_lazy('Post category', 'Category') verbose_name_plural = pgettext_lazy('Post categories', 'Categories') + class CategoryTranslation(models.Model): - name = models.CharField(max_length=250, verbose_name=pgettext_lazy('Noun, not personal name', 'Name'), blank=False) - language = models.CharField(max_length=5, verbose_name=_('Language (ISO)'), blank=False) - category = models.ForeignKey(Category, verbose_name = pgettext_lazy('Post category', 'Category'), blank=False, on_delete=models.CASCADE) + name = models.CharField(max_length=250, verbose_name=pgettext_lazy( + 'Noun, not personal name', 'Name'), blank=False) + language = models.CharField( + max_length=5, verbose_name=_('Language (ISO)'), blank=False) + category = models.ForeignKey(Category, verbose_name=pgettext_lazy( + 'Post category', 'Category'), blank=False, on_delete=models.CASCADE) def __str__(self): return self.name @@ -38,26 +46,42 @@ class CategoryTranslation(models.Model): class BlogPost(models.Model): - author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('Author'), on_delete=models.PROTECT) - title = models.CharField(max_length=100, verbose_name=pgettext_lazy('As in name', 'Title'), blank=False) - content = models.TextField(verbose_name=pgettext_lazy('Of post, comment, article, etc.', 'Content'), blank=False) - preview_image = models.ImageField(upload_to='weblog/preview_images/%Y/%m/%d/', blank=True, verbose_name=_('Preview image')) - preview_text = models.CharField(max_length=250, blank=True, verbose_name=_('Preview Text')) - original_language = models.CharField(max_length=5, verbose_name=_('Original language (ISO)'), blank=True) - slug = models.SlugField(max_length=100, verbose_name=_('Slug (URL)'), db_index=True, unique=True) - categories = models.ManyToManyField(Category, verbose_name=pgettext_lazy('Post categories', 'Categories'), blank=True) - pinned = models.BooleanField(verbose_name=_('Pin blog post'), default=False) - pin_priority = models.IntegerField(verbose_name=_('Pinned post priority (if pinned)'), default=0) - published = models.BooleanField(verbose_name=pgettext_lazy('Make post viewable', 'Published')) + author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_( + 'Author'), on_delete=models.PROTECT) + title = models.CharField(max_length=100, verbose_name=pgettext_lazy( + 'As in name', 'Title'), blank=False) + content = models.TextField(verbose_name=pgettext_lazy( + 'Of post, comment, article, etc.', 'Content'), blank=False) + preview_image = models.ImageField( + upload_to='weblog/preview_images/%Y/%m/%d/', blank=True, + verbose_name=_('Preview image')) + preview_text = models.CharField( + max_length=250, blank=True, verbose_name=_('Preview Text')) + original_language = models.CharField( + max_length=5, verbose_name=_('Original language (ISO)'), blank=True) + slug = models.SlugField(max_length=100, verbose_name=_( + 'Slug (URL)'), db_index=True, unique=True) + categories = models.ManyToManyField(Category, verbose_name=pgettext_lazy( + 'Post categories', 'Categories'), blank=True) + pinned = models.BooleanField( + verbose_name=_('Pin blog post'), default=False) + pin_priority = models.IntegerField(verbose_name=_( + 'Pinned post priority (if pinned)'), default=0) + published = models.BooleanField( + verbose_name=pgettext_lazy('Make post viewable', 'Published')) publish_date = models.DateTimeField(verbose_name=_('Publish date')) def get_absolute_url(self): if self.categories.all().count() > 0: category = self.categories.all()[0].slug - return reverse('weblog:PostView', kwargs={'category_slug': category, 'post_slug': self.slug}) + return reverse('weblog:PostView', + kwargs={'category_slug': category, + 'post_slug': self.slug}) else: - return reverse('weblog:PostView', kwargs={'category_slug': 'misc', 'post_slug': self.slug}) + return reverse('weblog:PostView', + kwargs={'category_slug': 'misc', + 'post_slug': self.slug}) def __str__(self): return self.title @@ -67,28 +91,41 @@ class BlogPost(models.Model): verbose_name = _('Blog Post') verbose_name_plural = _('Blog Posts') + class Translation(models.Model): - post = models.ForeignKey(BlogPost, verbose_name=pgettext_lazy('Noun, as in blog post', 'Post'), on_delete=models.CASCADE) - language = models.CharField(max_length=5, verbose_name=_('Language (ISO)'), blank=False) - title = models.CharField(max_length=100, verbose_name=pgettext_lazy('As in name', 'Title'), blank=False) - content = models.TextField(verbose_name=pgettext_lazy('Of post, comment, article, etc.', 'Content'), blank=False) - preview_image = models.ImageField(upload_to='weblog/preview_images/%Y/%m/%d/', blank=True, verbose_name=_('Preview image')) - preview_text = models.CharField(max_length=250, blank=True, verbose_name=_('Preview Text')) + post = models.ForeignKey(BlogPost, verbose_name=pgettext_lazy( + 'Noun, as in blog post', 'Post'), on_delete=models.CASCADE) + language = models.CharField( + max_length=5, verbose_name=_('Language (ISO)'), blank=False) + title = models.CharField(max_length=100, verbose_name=pgettext_lazy( + 'As in name', 'Title'), blank=False) + content = models.TextField(verbose_name=pgettext_lazy( + 'Of post, comment, article, etc.', 'Content'), blank=False) + preview_image = models.ImageField( + upload_to='weblog/preview_images/%Y/%m/%d/', blank=True, + verbose_name=_('Preview image')) + preview_text = models.CharField( + max_length=250, blank=True, verbose_name=_('Preview Text')) class Meta: verbose_name = _('Translation') verbose_name_plural = _('Translations') + class PostComment(models.Model): - author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('Author'), null=True, blank=True, on_delete=models.PROTECT) - post = models.ForeignKey(BlogPost, verbose_name=pgettext_lazy('Noun, as in blog post', 'Post'), on_delete=models.CASCADE) - content = models.TextField(verbose_name=pgettext_lazy('Of post, comment, article, etc.', 'Content'), blank=False) + author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_( + 'Author'), null=True, blank=True, on_delete=models.PROTECT) + post = models.ForeignKey(BlogPost, verbose_name=pgettext_lazy( + 'Noun, as in blog post', 'Post'), on_delete=models.CASCADE) + content = models.TextField(verbose_name=pgettext_lazy( + 'Of post, comment, article, etc.', 'Content'), blank=False) publish_date = models.DateTimeField(verbose_name=_('Publish date')) class Meta: verbose_name = pgettext_lazy('Noun', 'Comment') verbose_name_plural = pgettext_lazy('Noun', 'Comments') + class PostCommentForm(ModelForm): class Meta: model = PostComment diff --git a/weblog/templatetags/weblog_extras.py b/weblog/templatetags/weblog_extras.py index 777618d..2b4d830 100755 --- a/weblog/templatetags/weblog_extras.py +++ b/weblog/templatetags/weblog_extras.py @@ -25,22 +25,35 @@ MONTHS = ( register = template.Library() + @register.inclusion_tag('weblog/sidebar_categories.html') def get_sidebar_categories(selected_cat_slug=None): now = datetime.datetime.now() current_language = translation.get_language() + if current_language is None: current_language = settings.LANGUAGE_CODE context_dict = {'categories': [], 'selected_cat_slug': selected_cat_slug} + for raw_category in Category.objects.all(): next_category = {'name': raw_category.name, 'slug': raw_category.slug} - if CategoryTranslation.objects.filter(category=raw_category).count() > 0 and IS_MULTILINGUAL: - for category_translation in CategoryTranslation.objects.filter(category=raw_category): + + if CategoryTranslation.objects.filter( + category=raw_category).count() > 0 and IS_MULTILINGUAL: + + for category_translation in CategoryTranslation.objects.filter( + category=raw_category): + if current_language[0:2] == category_translation.language[0:2]: - next_category['name'] = category_translation.name + next_category['name'] = category_translation.name context_dict['categories'].append(next_category) - if BlogPost.objects.filter(published=True, publish_date__lte=now, categories=None).count() > 0: - context_dict['categories'].append({'name': pgettext_lazy('Posts without category', 'Uncategorized'), 'slug': 'misc'}) + + if BlogPost.objects.filter( + published=True, publish_date__lte=now, + categories=None).count() > 0: + context_dict['categories'].append({'name': pgettext_lazy( + 'Posts without category', 'Uncategorized'), 'slug': 'misc'}) + return context_dict @@ -52,22 +65,32 @@ def get_sidebar_archive(): oldest_post = BlogPost.objects.filter(published=True).reverse()[0] first_year = oldest_post.publish_date.year first_month = oldest_post.publish_date.month - newest_post = BlogPost.objects.filter(published=True, publish_date__lte=now)[0] + newest_post = BlogPost.objects.filter( + published=True, publish_date__lte=now)[0] latest_year = newest_post.publish_date.year latest_month = newest_post.publish_date.month c_month = first_month c_year = first_year archive = [] + while c_year <= latest_year: - year_posts = BlogPost.objects.filter(publish_date__year=c_year, publish_date__lte=now, published=True) + year_posts = BlogPost.objects.filter( + publish_date__year=c_year, publish_date__lte=now, published=True) + if year_posts.count() > 0: this_years_months = [] - while (c_year < latest_year or c_month <= latest_month) and c_month <= 12: - if year_posts.filter(publish_date__month=c_month, publish_date__lte=now, published=True).count() > 0: - this_years_months.append((c_month, MONTHS[c_month-1])) - c_month+=1 + + while (c_year < latest_year or c_month <= latest_month) \ + and c_month <= 12: + + if year_posts.filter( + publish_date__month=c_month, publish_date__lte=now, + published=True).count() > 0: + this_years_months.append((c_month, MONTHS[c_month - 1])) + c_month += 1 archive.append((c_year, this_years_months)) - c_year+=1 - c_month=1 + c_year += 1 + c_month = 1 archive.reverse() + return {'archive': archive} -- cgit v1.2.3 From 468d6c86a076623e5ea7c38cd8b2547e6f9f1529 Mon Sep 17 00:00:00 2001 From: Tiberiu Ichim Date: Sat, 5 Jan 2019 12:36:57 +0100 Subject: Comment publish date for comments, there's no migration to create that column --- weblog/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'weblog') diff --git a/weblog/models.py b/weblog/models.py index 1ccfbd0..dac8d1e 100755 --- a/weblog/models.py +++ b/weblog/models.py @@ -119,7 +119,7 @@ class PostComment(models.Model): 'Noun, as in blog post', 'Post'), on_delete=models.CASCADE) content = models.TextField(verbose_name=pgettext_lazy( 'Of post, comment, article, etc.', 'Content'), blank=False) - publish_date = models.DateTimeField(verbose_name=_('Publish date')) + # publish_date = models.DateTimeField(verbose_name=_('Publish date')) class Meta: verbose_name = pgettext_lazy('Noun', 'Comment') -- cgit v1.2.3