diff --git a/requirements.txt b/requirements.txt index d69b444..7461943 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,8 @@ python-decouple~=3.6 jetforce~=0.9.0 django-extensions~=3.2.1 +django-debug-toolbar~=3.6.0 +progressbar~=4.0.0 git+https://github.com/django-haystack/django-haystack.git git+https://tildegit.org/matthias/xapian-haystack.git diff --git a/thoughts/management/commands/update_html_content.py b/thoughts/management/commands/update_html_content.py new file mode 100644 index 0000000..2c96562 --- /dev/null +++ b/thoughts/management/commands/update_html_content.py @@ -0,0 +1,11 @@ +from progressbar import progressbar + +from django.core.management.base import BaseCommand + +from thoughts.models import Thought + +class Command(BaseCommand): + def handle(self, *args, **options): + for t in progressbar(Thought.objects.all()): + # Saving forces us to re-generate HTML content + t.save() diff --git a/thoughts/migrations/0007_thought_html_content_alter_thought_posted.py b/thoughts/migrations/0007_thought_html_content_alter_thought_posted.py new file mode 100644 index 0000000..637fca6 --- /dev/null +++ b/thoughts/migrations/0007_thought_html_content_alter_thought_posted.py @@ -0,0 +1,24 @@ +# Generated by Django 4.0.3 on 2022-09-17 17:54 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('thoughts', '0006_alter_thought_text'), + ] + + operations = [ + migrations.AddField( + model_name='thought', + name='html_content', + field=models.TextField(blank=True), + ), + migrations.AlterField( + model_name='thought', + name='posted', + field=models.DateTimeField(default=django.utils.timezone.now), + ), + ] diff --git a/thoughts/models.py b/thoughts/models.py index 63b3840..5f90156 100644 --- a/thoughts/models.py +++ b/thoughts/models.py @@ -8,16 +8,21 @@ from django.db import models from django.utils import timezone from django.utils.text import normalize_newlines from django.core.exceptions import ValidationError - +from django.template.loader import render_to_string class Thought(models.Model): text = models.CharField(max_length=140, blank=True) extended_text = models.TextField(blank=True) uuid = models.UUIDField(default=uuid.uuid4, editable=False) - posted = models.DateTimeField(auto_now_add=True) + posted = models.DateTimeField(default=timezone.now) timezone_offset = models.IntegerField() # The number of minutes behind UTC we were when this was posted media = models.FileField(upload_to="", blank=True) # A single image, video, or sound clip per post media_alt = models.TextField(blank=True) # An optional transcription of the Thought's media + html_content = models.TextField(blank=True) # The rendered text for this thought + + def save(self): + self.update_html_content(commit=False) + super().save() def get_media_type(self): if not self.media: @@ -40,6 +45,17 @@ class Thought(models.Model): def get_absolute_url(self): return f"/?show={str(self.uuid)}" + def get_html_content(self, authenticated=False): + return render_to_string("thoughts/thought.html", {"thought": self, "authenticated": authenticated}) + + def get_html_content_authenticated(self): + return self.get_html_content(authenticated=True) + + def update_html_content(self, commit=True): + self.html_content = self.get_html_content() + if commit: + self.save() + @classmethod def find(cls, identifier): try: diff --git a/thoughts/templates/thoughts/index.html b/thoughts/templates/thoughts/index.html index 91c0e66..e2932e9 100644 --- a/thoughts/templates/thoughts/index.html +++ b/thoughts/templates/thoughts/index.html @@ -56,60 +56,18 @@ {% endif %} - {% load tz %} - {% for thought in thoughts %} -
-
- {{ thought.text|urlize }} -
- - {% with file_type=thought.get_media_type %} - {% if file_type or thought.extended_text.strip %} -
- {% if thought.extended_text.strip %} - {{ thought.extended_text|urlize }} - {% endif %} - {% if file_type == "png" or file_type == "jpeg" or file_type == "jpg" %} - {{ thought.media_alt }} - {% elif file_type == "m4a" or file_type == "mp3" or file_type == "aac" %} - - {% if thought.media_alt %} - -
Transcript
-
{{ thought.media_alt|urlize }}
- {% endif %} - {% elif file_type == "mov" or file_type == "mp4" %} - - {% if thought.media_alt %} - -
Description
-
{{ thought.media_alt|urlize }}
- {% endif %} - {% endif %} -
- {% endif %} - {% endwith %} -
- - Link - - {% if thought.uuid == highlighted and authenticated %} - - Edit - - {% endif %} - - {% timezone thought.get_timezone %} - {{ thought.posted|time:"g:i a" }} - {{ thought.posted|date:"M d, Y" }}, - - UTC{{ thought.get_offset_hours }} - {% endtimezone %} - -
-
-
+
+ {% if thought.uuid == highlighted %} + {% if authenticated %} + {{ thought.get_html_content_authenticated }} + {% else %} + {{ thought.get_html_content}} + {% endif %} + {% else %} + {{ thought.html_content|safe }} + {% endif %} +
{% endfor %}