forked from sydney-linux-user-group/slug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_lists.py
136 lines (104 loc) · 3.66 KB
/
event_lists.py
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
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python
#
# -*- coding: utf-8 -*-
# vim: set ts=4 sw=4 et sts=4 ai:
# We have functions with loads of arguments.
# pylint: disable-msg=R0913
"""Helper module for obtaining lists of events"""
import config
config.setup()
from google.appengine.ext import db
from aeoid import users as openid
import datetime
import models
def fix_responses(user):
fedid = user.federated_identity()
if fedid:
openid_user_key = openid.UserInfo.get_by_identity_url(fedid)
if openid_user_key:
query = ("Select * from Response "
"WHERE created_by = :1")
resps = db.GqlQuery(query, openid_user_key)
for resp in resps:
resp.gcreated_by = user
resp.put()
def get_event_responses(event, user):
response = None
guests = []
if user:
fix_responses(user)
responses = event.responses.filter("gcreated_by = ",
user).order("created_on")
for resp in responses:
if not resp.guest:
response = resp
else:
guests.append(resp)
return response, guests
def get_eventlist_responses(event_list, user):
events = []
for event in event_list:
response, guests = get_event_responses(event, user)
events.append( (event, response, guests) )
return events
def get_future_events(
year=None, month=None, day=None, published_only=True, user=None,
count=100):
now = datetime.datetime.now()
year = year or now.year
month = month or now.month
day = day or now.day
query = ("SELECT * from Event "
"WHERE start > DATETIME(:1, :2, :3, 23, 59, 59) ")
if published_only:
query += "AND published = True "
query += "ORDER BY start"
future_events = db.GqlQuery(query, year, month, day).fetch(count)
return EventList(get_eventlist_responses(
future_events, user), "Coming soon")
def get_current_events(
year=None, month=None, day=None, published_only=True, user=None,
count=5):
now = datetime.datetime.now()
year = year or now.year
month = month or now.month
day = day or now.day
query = ("SELECT * from Event "
"WHERE end >= DATETIME(:1, :2, :3, 00, 00, 00) "
"AND end <= DATETIME(:1, :2, :3, 23, 59, 59) ")
if published_only:
query += "AND published = True "
query += "ORDER BY end"
current_events = db.GqlQuery(query, year, month, day).fetch(count)
return EventList(get_eventlist_responses(
current_events, user), "Happening today")
def get_next_event(year=None, month=None, day=None, hour=None, minute=None,
second=None):
now = datetime.datetime.now()
year = year or now.year
month = month or now.month
day = day or now.day
hour = hour or now.hour
minute = minute or now.minute
second = second or now.second
next_event = db.GqlQuery(
"SELECT * from Event " +
"WHERE start >= DATETIME(:1, :2, :3, :4, :5, :6) " +
"ORDER BY start", year, month, day, hour, minute, second).get()
return next_event
def get_event_lists(year=None, month=None, day=None, hour=None, minute=None,
second=None, published_only=True, user=None):
event_lists = []
event_lists.append(
get_current_events(year, month, day, published_only, user))
event_lists.append(
get_future_events(year, month, day, published_only, user))
return event_lists
class EventList(object):
"""A named list of events.
Arguments:
events: list of events
name: a name """
def __init__(self, events, name):
self.events = events
self.name = name