Skip to content
This repository
tree: c4930a0289
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 127 lines (95 sloc) 3.528 kb
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#!/usr/bin/python
#
# -*- coding: utf-8 -*-
# vim: set ts=4 sw=4 et sts=4 ai:

"""Module for creating and editing Event objects."""

# Python Imports
import os
try:
    import simplejson as json
except ImportError:
    import json

import datetime

# Django Imports
from django import http
from django import shortcuts
from django.views.decorators import http as method
from django.contrib.auth import decorators as auth

# Third Party imports
from dateutil import rrule
from datetime_tz import datetime_tz

# Our App imports
from usergroup import models
from usergroup import offers


def lastfridays():
    """Return 5 last Fridays of the month."""
    return list(x.date() for x in rrule.rrule(
         rrule.MONTHLY, interval=1, count=10, byweekday=(rrule.FR(-1)),
         dtstart=datetime.datetime.now()))

def meeting_times(date, starttime=(18, 30), endtime=(20, 00)):
    """Generate meeting start/end times on particular date."""
    #pylint: disable=W0142
    formatstring = "%B %d, %Y %I:%M%p"
    start = datetime.datetime.combine(date, datetime.time(*starttime))
    end = datetime.datetime.combine(date, datetime.time(*endtime))
    return start.strftime(formatstring), end.strftime(formatstring)



def get_templates():
    """Get all the markdown templates."""
    ret = []

    directory = "usergroup/templates/markdown"
    for filename in os.listdir(directory):
        if not filename.endswith('.md'):
            continue

        fullpath = os.path.join(directory, filename)
        name = filename.replace('.md', '')
        ret.append((name, file(fullpath).read()))
    return ret


@auth.login_required
@method.require_POST
def handler_offer_add(request, event_key):
    """Adds an offer to an event"""

    event = shortcuts.get_object_or_404(models.Event, pk=event_key)
    offer = shortcuts.get_object_or_404(
            models.TalkOffer, pk=request.POST.get('id', -1))

    talk = models.LightningTalk(event=event, offer=offer)
    talk.save()

    response = http.HttpResponse()
    response['Content-Type'] = 'application/javascript'
    response.write(json.dumps({'key': str(talk.key())}))
    return response


@auth.login_required
@method.require_POST
def handler_offer_remove(request, event_key):
    """Removes an offer from an event."""

    event = shortcuts.get_object_or_404(models.Event, pk=event_key)
    talk = shortcuts.get_object_or_404(
            models.LightningTalk, pk=request.POST.get('id', -1))
    assert talk.event == event

    talk.delete()

    response = http.HttpResponse()
    response['Content-Type'] = 'application/javascript'
    response.write(json.dumps({'deleted': str(True)}))
    return response


@auth.login_required
@method.require_GET
def handler(request, event_key):
    """Handler for viewing the edit form."""

    assert request.user.is_staff

    try:
        if event_key == 'add':
            event = models.Event(created_by=request.user)
        else:
            event = shortcuts.get_object_or_404(models.Event, pk=event_key)
    except IndexError:
        return shortcuts.redirect('/events')

    template_values = {}
    if event:
        template_values['agenda'] = offers.get_event_agenda(event)
    else:
        template_values['agenda'] = None

    template_values['event'] = event
    template_values['fridays'] = lastfridays()
    template_values['templates'] = get_templates()
    template_values['offers'] = models.TalkOffer.objects.all()[:100]

    return shortcuts.render(
            request, 'editevent.html', template_values)
Something went wrong with that request. Please try again.