diff options
Diffstat (limited to 'app/posts/models.py')
-rw-r--r-- | app/posts/models.py | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/app/posts/models.py b/app/posts/models.py index b92518c..8312040 100644 --- a/app/posts/models.py +++ b/app/posts/models.py @@ -33,6 +33,13 @@ from jrnl.models import Entry from utils.util import render_images, render_products, parse_video, markdown_to_html +class PostType(models.IntegerChoices): + FIELD_TEST = 0, ('field test') + REVIEW = 1, ('review') + ESSAY = 2, ('essay') + SRC = 3, ('src') + JRNL = 4, ('jrnl') + class Post(models.Model): old_id = models.IntegerField(blank=True, null=True) title = models.CharField(max_length=200) @@ -59,14 +66,7 @@ class Post(models.Model): TEMPLATES = ( (0, 'single'), ) - POST_TYPE = ( - (0, 'field test'), - (1, 'review'), - (2, 'essay'), - (3, 'src'), - (4, 'jrnl'), - ) - post_type = models.IntegerField(choices=POST_TYPE, default=0) + post_type = models.IntegerField(choices=PostType.choices, default=PostType.JRNL) template_name = models.IntegerField(choices=TEMPLATES, default=0) has_video = models.BooleanField(blank=True, default=False) has_code = models.BooleanField(blank=True, default=False) @@ -75,8 +75,8 @@ class Post(models.Model): field_notes = models.ManyToManyField(FieldNote, blank=True) related = models.ManyToManyField(RelatedPost, blank=True) point = models.PointField(null=True, blank=True) - topics = models.ManyToManyField(Category, blank=True) location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True) + topics = models.ManyToManyField(Category, blank=True) originally_published_by = models.CharField(max_length=400, null=True, blank=True) originally_published_by_url = models.CharField(max_length=400, null=True, blank=True) field_notes = models.ManyToManyField(FieldNote, blank=True) @@ -92,11 +92,13 @@ class Post(models.Model): def get_absolute_url(self): if self.post_type == 0: - return reverse('guides:detail', kwargs={"slug": self.slug}) + return reverse('guides:reviews:review-detail', kwargs={"slug": self.slug}) if self.post_type == 1: - return reverse('reviews:review-detail', kwargs={"slug": self.slug}) + return reverse('guides:reviews:review-detail', kwargs={"slug": self.slug}) if self.post_type == 2: return reverse('essays:detail', kwargs={"slug": self.slug}) + if self.post_type == 3: + return reverse('src:detail', kwargs={"slug": self.slug}) def comment_period_open(self): return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date @@ -166,6 +168,25 @@ class Post(models.Model): super(Post, self).save(*args, **kwargs) + try: + self.location = Location.objects.filter(geometry__contains=self.point).get() + except Location.DoesNotExist: + raise forms.ValidationError("There is no location associated with that point, add it: %sadmin/locations/location/add/" % (settings.BASE_URL)) + if created and not self.featured_image: + self.featured_image = LuxImage.objects.latest() + old = type(self).objects.get(pk=self.pk) if self.pk else None + if old and old.featured_image != self.featured_image: # Field has changed + s = LuxImageSize.objects.get(name="featured_jrnl") + ss = LuxImageSize.objects.get(name="picwide-med") + self.featured_image.sizes.add(s) + self.featured_image.sizes.add(ss) + self.featured_image.save() + if old.title != self.title or old.slug != self.slug: + related, c = RelatedPost.objects.get_or_create(model_name=self.get_content_type(), entry_id = self.id, pub_date=self.pub_date) + related.title = self.title + related.slug = self.slug + related.save() + class PostModerator(CommentModerator): ''' Moderate everything except people with multiple approvals @@ -219,6 +240,7 @@ class PostSitemap(Sitemap): """ for p in src: s, created = Post.objects.get_or_create( + old_id=p.id, title=p.title, slug=p.slug, body_markdown=p.body_markdown, @@ -231,6 +253,10 @@ for p in src: ) print(p) for t in p.topics.all(): - c = Category.objects.get(slug=t.slug) + c,created = Category.objects.get_or_create( + slug=t.slug, + name=t.name, + pluralized_name=t.pluralized_name + ) s.topics.add(c) """ |