very archive view

 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
def create_archive_list(_query):
    """
    given a sorted queryset, return a list in the format:
    
    archive_list = [{'2009': [{'December': ['entry1', 'entry2']},
                              {'January': ['entry3', 'entry4']}
                              ]
                     },
                    
                    {'2008': [{'January': ['entry5', 'entry6']},
                              ]
                     }
                    ]
    """

    archive_list = []
    tmp_months_list = []
    tmp_year_list = []

    for item in _query:
        if item.pub_date.year not in tmp_year_list:
            tmp_year_list.append(item.pub_date.year)
            tmp_dict = {}
            tmp_dict[item.pub_date.year] = []
            archive_list.append(tmp_dict)
        else:
            pass

    for entry in _query:
        for item in archive_list:
            _year = entry.pub_date.year
            if _year in item:
                _tmp_month = entry.pub_date.strftime("%B")
                # make a dictionary with the month name as a key and an empty list as a value
                tmp_dict = {}
                tmp_dict[_tmp_month] = []

                if tmp_dict not in item[_year]:
                    item[_year].append(tmp_dict)
                
                    
    for entry in _query:
        # dict in list
        for item in archive_list:
            # year of entry
            _year = entry.pub_date.year
            if _year in item:
                _year_list = item[_year]
                for _dict_month in _year_list:
                    _tmp_month = entry.pub_date.strftime("%B")
                    if _tmp_month in _dict_month:
                        _dict_month[_tmp_month].append(entry)
                        
    return archive_list


def very_archive(request):
    query = Entry.objects.all().order_by('-pub_date')
    object_years_dict = create_archive_list(query)
    
    return render_to_response('ideas/ideas_index.html', {'archive_list': object_years_dict})

More like this

  1. very archive template by stuntgoat 3 years, 7 months ago
  2. Using reverse() to do redirects by ubernostrum 5 years, 9 months ago
  3. Avoid widows using a template filter by jcroft 6 years, 2 months ago
  4. Owner required decorator by polarbear 4 years, 10 months ago
  5. Archive week beginning on monday by fivethreeo 4 years, 5 months ago

Comments

(Forgotten your password?)