archive per week
This commit is contained in:
parent
b0bbe1805b
commit
dd2ce36765
5 changed files with 89 additions and 6 deletions
|
@ -83,6 +83,16 @@ class Item(models.Model):
|
||||||
)
|
)
|
||||||
return week, archive
|
return week, archive
|
||||||
|
|
||||||
|
def get_week(self):
|
||||||
|
return int(self.published.strftime('%W'))
|
||||||
|
|
||||||
|
def get_year(self):
|
||||||
|
return int(self.published.strftime('%Y'))
|
||||||
|
|
||||||
|
def get_monday(self):
|
||||||
|
d = '%s-W%s' % (self.get_year(), self.get_week())
|
||||||
|
return datetime.strptime(d + '-1', "%Y-W%W-%w").strftime('%Y-%m-%d')
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('item', kwargs={'id': self.id})
|
return reverse('item', kwargs={'id': self.id})
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import json
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.timezone import datetime, timedelta
|
from django.utils.timezone import datetime, timedelta
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render, redirect
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.utils.html import mark_safe
|
from django.utils.html import mark_safe
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
@ -23,6 +23,31 @@ def get_now():
|
||||||
return timezone.make_aware(datetime.now(), timezone.get_default_timezone())
|
return timezone.make_aware(datetime.now(), timezone.get_default_timezone())
|
||||||
|
|
||||||
|
|
||||||
|
def get_monday(date):
|
||||||
|
d = date.strftime('%Y-W%W-1')
|
||||||
|
return datetime.strptime(d, "%Y-W%W-%w").strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
|
||||||
|
def format_week(week):
|
||||||
|
a = datetime.strptime(week, '%Y-%m-%d')
|
||||||
|
b = (a + timedelta(days=6))
|
||||||
|
fmt = '%b %d'
|
||||||
|
a = a.strftime(fmt)
|
||||||
|
b = b.strftime(fmt)
|
||||||
|
return '%s - %s' % (a, b)
|
||||||
|
|
||||||
|
|
||||||
|
def get_weeks(archive):
|
||||||
|
weeks = sorted(set(archive.values_list('year', 'week')))
|
||||||
|
weeks = [datetime.strptime('%s-W%s-1' % w, "%Y-W%W-%w").strftime('%Y-%m-%d') for w in weeks]
|
||||||
|
weeks = [{
|
||||||
|
'date': week,
|
||||||
|
'year': week.split('-')[0],
|
||||||
|
'title': format_week(week)
|
||||||
|
} for week in weeks]
|
||||||
|
return weeks
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
context = default_context(request)
|
context = default_context(request)
|
||||||
now = request.GET.get("now")
|
now = request.GET.get("now")
|
||||||
|
@ -36,7 +61,7 @@ def index(request):
|
||||||
week, archive = models.Item.public(now)
|
week, archive = models.Item.public(now)
|
||||||
context['items'] = week
|
context['items'] = week
|
||||||
if archive.exists():
|
if archive.exists():
|
||||||
context['archive'] = '/_%s/' % get_now().year
|
context['archive'] = '/_%s/' % get_monday(get_now() - timedelta(days=7))
|
||||||
if now:
|
if now:
|
||||||
context['now'] = now
|
context['now'] = now
|
||||||
context['previous_week'] = (now - timedelta(days=7)).strftime(TS_FORMAT)
|
context['previous_week'] = (now - timedelta(days=7)).strftime(TS_FORMAT)
|
||||||
|
@ -44,11 +69,26 @@ def index(request):
|
||||||
return render(request, 'index.html', context)
|
return render(request, 'index.html', context)
|
||||||
|
|
||||||
|
|
||||||
def archive(request, year=None, week=None):
|
def archive(request, year=None, month=None, day=None, week=None):
|
||||||
context = default_context(request)
|
context = default_context(request)
|
||||||
qs = models.Item.public()
|
_, archive = models.Item.public()
|
||||||
week, archive = models.Item.public()
|
if year and month and day:
|
||||||
context['items'] = archive
|
date = datetime.strptime('%s-%s-%s' % (year, month, day), '%Y-%m-%d')
|
||||||
|
week = int(date.strftime('%W'))
|
||||||
|
year = int(year)
|
||||||
|
archive_week = archive.filter(year=year, week=week).order_by('published')
|
||||||
|
context['weeks'] = get_weeks(archive)
|
||||||
|
context['this_week'] = date.strftime('%Y-%m-%d')
|
||||||
|
context['this_year'] = date.strftime('%Y')
|
||||||
|
elif week:
|
||||||
|
week = int(week)
|
||||||
|
year = int(year)
|
||||||
|
week = datetime.strptime('%s-W%s-1' % (year, week), "%Y-W%W-%w").strftime('%Y-%m-%d')
|
||||||
|
return redirect('/_%s/' % week)
|
||||||
|
elif year:
|
||||||
|
week = datetime.strptime('%s-W1-1' % year, "%Y-W%W-%w").strftime('%Y-%m-%d')
|
||||||
|
return redirect('/_%s/' % week)
|
||||||
|
context['items'] = archive_week
|
||||||
return render(request, 'archive.html', context)
|
return render(request, 'archive.html', context)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -134,6 +134,10 @@ header {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.weeks {
|
||||||
|
text-align: center;
|
||||||
|
padding-bottom: 32px;
|
||||||
|
}
|
||||||
|
|
||||||
nav.overlay {
|
nav.overlay {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
|
|
@ -18,5 +18,32 @@
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
{% if weeks %}
|
||||||
|
<div class="weeks">
|
||||||
|
<div>
|
||||||
|
{% for week in weeks %}
|
||||||
|
{% ifchanged week.year %}
|
||||||
|
</div>
|
||||||
|
<h1>{{ week.year }}</h1>
|
||||||
|
<div class="year-details-{{ week.year }}" {% if week.year != this_year %} style="display: none"{% endif %}>
|
||||||
|
{% endifchanged %}
|
||||||
|
{% if week.date == this_week %}
|
||||||
|
<b>{{ week.title }}</b><br>
|
||||||
|
{% else %}
|
||||||
|
<a href="/_{{ week.date }}/">{{ week.title }}</a><br>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
document.querySelectorAll('.weeks h1').forEach(h1 => {
|
||||||
|
h1.addEventListener('click', event => {
|
||||||
|
var year = event.target.innerText
|
||||||
|
var details = document.querySelector('.year-details-' + year)
|
||||||
|
details.style.display = details.style.display == 'none' ? '' : 'none'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,9 @@ urlpatterns = [
|
||||||
path('register/', user_views.register, name='register'),
|
path('register/', user_views.register, name='register'),
|
||||||
path('comment/publish/', item_views.publish_comment, name='publish-comment'),
|
path('comment/publish/', item_views.publish_comment, name='publish-comment'),
|
||||||
path('comment/', item_views.comment, name='comment'),
|
path('comment/', item_views.comment, name='comment'),
|
||||||
|
path('_<int:year>-<int:month>-<int:day>/', item_views.archive, name='archive'),
|
||||||
path('_<int:year>/', item_views.archive, name='archive'),
|
path('_<int:year>/', item_views.archive, name='archive'),
|
||||||
|
path('_<int:year>/<int:week>/', item_views.archive, name='archive'),
|
||||||
path('<int:id>/', item_views.item, name='item'),
|
path('<int:id>/', item_views.item, name='item'),
|
||||||
path('<str:slug>/', page_views.page, name='page'),
|
path('<str:slug>/', page_views.page, name='page'),
|
||||||
path('', item_views.index, name='index'),
|
path('', item_views.index, name='index'),
|
||||||
|
|
Loading…
Reference in a new issue