add language field to text model, output en and zh texts separately, refs #30

This commit is contained in:
Sanjay B 2021-10-28 15:42:34 +05:30
parent 0c10a6ed2e
commit 21dab42524
5 changed files with 44 additions and 6 deletions

View file

@ -3,10 +3,17 @@
{% block main %} {% block main %}
<div class="texts"> <div class="texts">
{% for text in texts %} {% for text in en_texts %}
<div class="text"> <div class="text en">
<h1><a href="{{ text.get_absolute_url }}">{{ text.title | safe }}</a></h1> <h1><a href="{{ text.get_absolute_url }}">{{ text.title | safe }}</a></h1>
<h2>{{ text.byline|striptags }}</h2> <h2>{{ text.byline|striptags }}</h2>
</div> </div>
{% endfor %} {% endfor %}
{% for text in zh_texts %}
<div class="text zh">
<h1><a href="{{ text.get_absolute_url }}">{{ text.title | safe }}</a></h1>
<h2>{{ text.byline|striptags }}</h2>
</div>
{% endfor %}
</div>
{% endblock %} {% endblock %}

View file

@ -9,11 +9,12 @@ class TextAdmin(admin.ModelAdmin):
'__str__', '__str__',
'item', 'item',
'edit', 'edit',
'language',
'slug', 'slug',
'public', 'public',
'position', 'position',
) )
list_editable = ['public', 'language']
def item(self, obj): def item(self, obj):
return obj.data.get('item') return obj.data.get('item')
def edit(self, obj): def edit(self, obj):

View file

@ -0,0 +1,23 @@
# Generated by Django 3.2.7 on 2021-10-28 10:07
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('text', '0007_text_annotations'),
]
operations = [
migrations.AddField(
model_name='text',
name='language',
field=models.CharField(choices=[('en', 'EN'), ('zh', 'ZH')], default='en', max_length=8),
),
migrations.AlterField(
model_name='text',
name='annotations',
field=models.JSONField(blank=True, default=dict, editable=False),
),
]

View file

@ -32,10 +32,15 @@ class Page(models.Model):
return '/' + settings.URL_PREFIX[:-1] return '/' + settings.URL_PREFIX[:-1]
LANGUAGE_CHOICES = (
('en', 'EN'),
('zh', 'ZH'),
)
class Text(models.Model): class Text(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True) modified = models.DateTimeField(auto_now=True)
language = models.CharField(choices=LANGUAGE_CHOICES, max_length=8, default='en')
slug = models.SlugField() slug = models.SlugField()
public = models.BooleanField(default=False) public = models.BooleanField(default=False)
position = models.IntegerField(default=0) position = models.IntegerField(default=0)

View file

@ -36,7 +36,9 @@ def about(request):
def texts(request): def texts(request):
context = {} context = {}
context['texts'] = models.Text.objects.filter(public=True).order_by('position', 'created') all_texts = models.Text.objects.filter(public=True).order_by('position', 'created')
context['en_texts'] = all_texts.filter(language='en')
context['zh_texts'] = all_texts.filter(language='zh')
return render(request, 'texts.html', context) return render(request, 'texts.html', context)
def text(request, slug): def text(request, slug):