summaryrefslogtreecommitdiff
path: root/app/notes/build.py
blob: 4923baff4c08355e4d558bb711b546af9f7fd22d (plain)
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
58
59
60
61
62
63
64
65
66
67
from builder.base import *


class BuildNotes(Build):
    def build(self):
        self.build_archive()
        self.build_archive_year()
        self.build_archive_month()
        self.build_detail_pages()

    def queryset(self):
        return self.get_model().objects.all().order_by('-date_created')

    def get_model(self):
        return get_model('notes', 'note')

    def build_detail_pages(self):
        '''
        Grab all the notes, render them to a template string and write that out to the filesystem
        '''
        for entry in self.queryset():
            c = Context({'object': entry, 'MEDIA_URL': settings.BAKED_MEDIA_URL, 'IMAGES_URL': settings.BAKED_IMAGES_URL, 'SITE_URL':settings.SITE_URL})
            t = render_to_string('details/note.html', c).encode('utf-8')
            path = 'field-notes/%s/' % (entry.date_created.strftime("%Y/%m").lower())
            self.write_file(path, t, 'html', entry.slug)
            s = render_to_string('details/note.txt', c).encode('utf-8')
            self.write_file(path, s, 'txt', entry.slug)

    def build_archive(self):
        path = 'field-notes/'
        c = Context({
            'object_list': self.queryset(),
            'MEDIA_URL': settings.BAKED_MEDIA_URL,
            'IMAGES_URL': settings.BAKED_IMAGES_URL
        })
        t = render_to_string('archives/notes.html', c).encode('utf-8')
        self.write_file(path, t)

    def build_archive_year(self):
        note = self.get_model()
        years = note.objects.dates('date_created', 'year')
        for year in years:
            year = year.strftime('%Y')
            qs = note.objects.filter(date_created__year=year).order_by('-date_created')
            c = Context({
                'year': year,
                'object_list': qs
            })
            t = render_to_string('archives/notes_date.html', c).encode('utf-8')
            fpath = 'field-notes/%s/' % (year)
            self.write_file(fpath, t)

    def build_archive_month(self):
        note = self.get_model()
        months = note.objects.dates('date_created', 'month')
        for m in months:
            year = m.strftime('%Y')
            month = m.strftime('%m')
            qs = note.objects.filter(date_created__year=year, date_created__month=month).order_by('-date_created')
            c = Context({
                'month': month,
                'year': year,
                'object_list': qs,
            })
            t = render_to_string('archives/notes_date.html', c).encode('utf-8')
            fpath = 'field-notes/%s/%s/' % (year, month)
            self.write_file(fpath, t)