-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproposal.py
More file actions
98 lines (91 loc) · 3.61 KB
/
proposal.py
File metadata and controls
98 lines (91 loc) · 3.61 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
"""CfP Manager - proposal submission"""
import webapp2
import urllib
import json
import os
from datetime import datetime
from urlparse import urljoin
from models import Conference
from models import Speaker
from models import Proposal
from settings import *
class ProposalHandler(webapp2.RequestHandler):
def getUrl(self, url):
"""Construct a full URL from one starting with /"""
referer = os.environ.get('HTTP_REFERER')
return urljoin(referer, url)
def success(self):
"""Success return"""
jsonReply = self.request.get("json-reply")
if jsonReply:
self.response.set_status(200)
self.response.headers["Content-Type"] = "application/json"
self.response.headers.add_header(
"Access-Control-Allow-Origin", "*"
)
self.response.out.write(json.dumps({"message": "success"}))
else:
successUrl = str(self.request.get("success-url"))
webapp2.redirect(self.getUrl(successUrl), response=self.response)
def error(self, text, status):
"""Error handling"""
jsonReply = self.request.get("json-reply")
if jsonReply:
self.response.set_status(status)
self.response.headers["Content-Type"] = "application/json"
self.response.headers.add_header(
"Access-Control-Allow-Origin", "*"
)
self.response.out.write(json.dumps({"message": text}))
else:
errorUrl = str(self.request.get("error-url"))
if "?" in errorUrl:
errorUrl = errorUrl + "&"
else:
errorUrl = errorUrl + "?"
errorUrl = errorUrl + "message=" + urllib.quote(text, safe="")
webapp2.redirect(self.getUrl(errorUrl), response=self.response)
def post(self, confid):
"""Accept proposals via POST requests"""
# get parameters from request
name = self.request.get("name")
surname = self.request.get("surname")
email = self.request.get("email")
bio = self.request.get("bio")
useOld = self.request.get("use-old")
title = self.request.get("title")
abstract = self.request.get("abstract")
duration = int(self.request.get("duration"))
comment = self.request.get("comment")
# timestamp 'now'
now = datetime.now()
# search for conference
conference = Conference.get_by_id(confid)
if not conference:
self.error("Conference not found", 404)
return
# search for speaker?
speakerKey = None
if useOld:
# get speakers for email
speakers = Speaker.query(Speaker.email == email)
# and order by modified-date (reverse) to get the last one
speakers = speakers.order(-Speaker.modified).iter()
if speakers.has_next():
speaker = speakers.next()
speakerKey = speaker.key
if not speakerKey:
# create speaker object
speaker = Speaker(name=name, surname=surname, email=email,
bio=bio, created=now, modified=now)
speakerKey = speaker.put()
# submit proposal
proposal = Proposal(parent=conference.key, speaker=speakerKey,
abstract=abstract, duration=duration,
title=title, comment=comment, created=now,
modified=now)
proposalKey = proposal.put()
if proposalKey:
self.success()
else:
self.error("Error when storing proposal", 500)