Commits

Ruslan Keba committed 1be2848

initial

Comments (0)

Files changed (5)

__init__.py

Empty file added.
+# -*- coding: utf-8 -*-
+
+MONTH_NAMES = (
+    'января',
+    'февраля',
+    'марта',
+    'апреля',
+    'мая',
+    'июня',
+    'июля',
+    'августа',
+    'сентября',
+    'октября',
+    'ноября',
+    'декабря'
+)
+
+WEEKDAY_NAMES = (
+    'понедельник',
+    'вторник',
+    'среда',
+    'четверг',
+    'пятница',
+    'суббота',
+    'воскресение'
+)
+
+
+def humanized_date(d):
+    return '%d %s, %s' % (d.day, MONTH_NAMES[d.month-1], WEEKDAY_NAMES[d.weekday()])
+
+
+
+
+
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+from https://gist.github.com/710689
+"""
+
+import re
+from hashlib import md5
+
+import markdown
+
+def gfm(text):
+    # Extract pre blocks.
+    extractions = {}
+    def pre_extraction_callback(matchobj):
+        digest = md5(matchobj.group(0)).hexdigest()
+        extractions[digest] = matchobj.group(0)
+        return "{gfm-extraction-%s}" % digest
+    pattern = re.compile(r'<pre>.*?</pre>', re.MULTILINE | re.DOTALL)
+    text = re.sub(pattern, pre_extraction_callback, text)
+
+    # Prevent foo_bar_baz from ending up with an italic word in the middle.
+    def italic_callback(matchobj):
+        s = matchobj.group(0)
+        if list(s).count('_') >= 2:
+            return s.replace('_', '\_')
+        return s
+    pattern = re.compile(r'^(?! {4}|\t)\w+(?<!_)_\w+_\w[\w_]*', re.MULTILINE | re.UNICODE)
+    text = re.sub(pattern, italic_callback, text)
+
+    # In very clear cases, let newlines become <br /> tags.
+    def newline_callback(matchobj):
+        if len(matchobj.group(1)) == 1:
+            return matchobj.group(0).rstrip() + '  \n'
+        else:
+            return matchobj.group(0)
+    pattern = re.compile(r'^[\w\<][^\n]*(\n+)', re.MULTILINE | re.UNICODE)
+    text = re.sub(pattern, newline_callback, text)
+
+    # Insert pre block extractions.
+    def pre_insert_callback(matchobj):
+        return '\n\n' + extractions[matchobj.group(1)]
+    text = re.sub(r'{gfm-extraction-([0-9a-f]{32})\}', pre_insert_callback, text)
+
+    return markdown.markdown(text)
+# -*- coding: utf-8 -*-
+
+from django.db import models
+from django.db.models import permalink
+from django.utils.safestring import mark_safe
+
+from gfm import gfm
+from dates import humanized_date
+
+
+class Club(models.Model):
+    title = models.CharField(max_length=50)
+    is_default = models.BooleanField()
+
+    def __unicode__(self):
+        return u'<Club \'{0}\''.format(self.title)
+
+    @staticmethod
+    def get_default():
+        return Club.objects.get(is_default=True)
+
+
+class Event(models.Model):
+
+    MARKUP_GFM = 0
+    MARKUP_HTML = 1
+
+    MARKUP_CHOICES = (
+        (MARKUP_GFM,  'Guthub Flavored Markdown'),
+        (MARKUP_HTML, 'Raw HTML')
+        )
+
+    club = models.ForeignKey(Club, default=Club.get_default)
+    title = models.CharField(max_length=50)
+    summary = models.TextField(blank=True, null=True)
+    markup = models.IntegerField(choices=MARKUP_CHOICES, default=MARKUP_GFM)
+    important = models.BooleanField(default=False)
+    created = models.DateField(auto_now_add=True)
+    date_begin = models.DateField()
+    date_end   = models.DateField(blank=True, null=True)
+    poster_url = models.URLField(verify_exists=False, blank=True, null=True)
+    album_url   = models.CharField(max_length=100, blank=True, null=True)
+    vkontakte_event = models.CharField(max_length=50, blank=True, null=True, help_text='example: event1234567')
+
+    class Meta:
+        ordering = ('-date_begin',)
+
+    def __unicode__(self):
+        return u'<Event \'{0}\''.format(self.title)
+
+    @permalink
+    def get_absolute_url(self):
+        return 'djclub.views.events', self.id
+
+    def summary_html(self):
+        if self.markup == self.MARKUP_GFM:
+            return mark_safe(gfm(self.summary))
+        else:
+            return mark_safe(self.summary)
+
+    def humanized_date_begin(self):
+        return humanized_date(self.date_begin)
+
+    def humanized_date_end(self):
+        return humanized_date(self.date_end) if self.date_end else None
+
+
+# -*- coding: utf-8 -*-
+
+from datetime import datetime, timedelta
+
+from django.shortcuts import render_to_response
+from django.template.context import RequestContext
+from django.views.decorators.cache import cache_page, cache_control
+
+from models import Club, Event
+
+@cache_control(public=True)
+@cache_page(30*60)
+def index(request, club=None, template=None):
+    events = _get_last_events(club or Club.get_default())
+    return render_to_response(template or 'djclub/index.html', RequestContext(request, dict(events=events)))
+
+def _get_last_events(club):
+    # select events with date_begin>now-5days
+    from_date = datetime.now()-timedelta(days=5)
+    events = Event.objects.filter(club=club).filter(date_begin__gte=from_date).order_by('-date_begin')
+
+    if not events:
+        # otherwise just last 10 events
+        events = Event.objects.filter(club=club).order_by('-date_begin')[:10]
+
+    return events