Post is outdated

Post #394 written by Khodok in Blog Updates

Content

Post is outdated

no, this post isn’t.. yet anyway
I added the ability to mark a post as outdated a long time ago, I actually don’t remember when.
Since the new design was made though, it wasn’t shown anywhere that a post was.
Since it’s one of the few things that are often shown properly to the reader in posts and stuff, I figured it wasn’t a bad idea to implement it from scratch, but this time do it well.
That’s it for non nerds 😄

This is how it looks now

This is from the post “Design
How it looks

The nerdy part

For a few months now, I’ve been actually trying to make my code a bit better overall. It still sucks, even though I’m a certified computer scientist now.
So here is how I implemented the new outdated system:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Post(Model):
    ...
    is_content_outdated = models.TextField(default="", blank=True, help_text="Is Post content's outdated")
    is_content_outdated_date = models.DateTimeField(blank=True, null=True, help_text="Outdated date")

    def get_absolute_outdated_url(self):
        return reverse("blog:post_is_outdated", kwargs={"slug": self.slug})

    # Property that gets the first line of body field
    @property
    def first_line(self):
        """First Line Post"""
        return self.body.split("\n")[0]

    # Property that removes anything between ![] in the first line
    @property
    def first_line_no_img(self):
        """First Line No Img Post"""
        return re.sub(r"!\[.*?\]", "", self.first_line)

    # Property that gets everything after the first line
    @property
    def after_first_line(self):
        """After First Line Post"""
        return self.body.split("\n")[1:]

    # Property that adds "This post is outdated" just after the first line
    @property
    def first_line_outdated(self):
        """First Line Outdated Post"""
        return (
            self.first_line
            + format_html(
                f'<div class="admonition outdated">'
                '<p class="admonition-title">This post is outdated as of {0}</p>'
                "<p>{1}</p></div>",
                self.is_content_outdated_date.strftime("%Y-%m-%d"),
                mark_safe(self.is_content_outdated),  # skipcq: BAN-B308
            )
            + "\n"
            + "\n".join(self.after_first_line)
        )

    # Property that defines the body_content depending on if it's outdated or not
    @property
    def body_content(self):
        """Body Content Post"""
        body = self.body
        if self.is_content_outdated_date is not None:
            body = self.first_line_outdated
        return body

    # Create a property that returns the markdown instead
    @property
    def formatted_markdown(self):
        """Formatted Markdown Post"""
        return markdownify(self.body_content)
Python
1
    path("outdated/", PostIsOutdatedUpdateView.as_view(), name="post_is_outdated"),
Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@superuser_required()
class PostIsOutdatedUpdateView(UpdateView):
    """PostIsOutdatedUpdateView

    View to mark a post as outdated
    """

    model = Post
    template_name = "blog/post_confirm_outdated.html"
    form_class = PostMarkOutdatedForm

    def form_valid(self, form):
        if form.instance.is_content_outdated_date:
            form.instance.is_content_outdated_date = form.instance.is_content_outdated_date
        else:
            form.instance.is_content_outdated_date = timezone.now()
        form.instance.save()
        return super().form_valid(form)

    def get_context_data(self, **kwargs):
        """get_context_data"""
        context = super().get_context_data(**kwargs)
        context["title"] = "Mark post as outdated"
        return context
Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class PostMarkOutdatedForm(forms.ModelForm):
"""PostMarkOutdatedForm

A form to mark a Post as outdated

Args:
    forms ([type]): [description]
"""

is_content_outdated_date = forms.SplitDateTimeField(
    required=False,
    input_date_formats=["%Y-%m-%d"],
    input_time_formats=["%H:%M", "%H:%M"],
    widget=forms.SplitDateTimeWidget(
        date_attrs={"type": "date"},
        date_format="%Y-%m-%d",
        time_attrs={"type": "time"},
        time_format="%H:%M",
    ),
)

class Meta:
    """Meta class for PostMarkOutdatedForm ModelForm"""

    model = Post
    fields = ("is_content_outdated", "is_content_outdated_date")
Comments

Please Log in to leave a comment.

No comments yet.