3 # UNICEF SMS Phone Application
5 # Copyright (C) 2007 Benjamin Mako Hill <mako@atdot.cc>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the Affero General Public License as published
9 # by the Free Software Foundation, either version 1 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the Affero General Public License
18 # along with this program. If not, see
19 # <http://http://www.affero.org/oagpl.html>.
26 # recalculate the path based on the location of the file
27 from jinja import Environment, FileSystemLoader
28 jinja_env = Environment(loader=FileSystemLoader(os.path.dirname(__file__) + "/templates/"))
30 def render(filename, vars={}):
31 """Render Jinja Templates"""
33 web.header("Content-Type","text/html; charset=utf-8")
35 tmpl = jinja_env.get_template(filename)
38 vars['home'] = web.ctx.home
39 vars['homepath'] = web.ctx.homepath
40 #vars['session'] = web.ctx.environ['com.saddi.service'].session
42 print tmpl.render(vars)
44 # the url map for the application
45 urls = ( '/?', 'index',
46 '/?new_name', 'new_name',
47 '/?show_names', 'show_names',
48 '/?questionaires', 'questionaires',
49 '/?edit_questions/(\d+)', 'edit_questions')
57 render('new_name.tmpl')
64 errors.append('You must provide a name.')
65 if not input.phone_number:
66 errors.append('You must provide a phone number.')
67 elif not re.match(r'[0-9\+\- ]', input.phone_number):
68 errors.append('Your phone number must be only numbers.')
71 for error in self.validate_phone(input.phone_number):
74 render('new_name.tmpl', locals())
76 # try to save it to the database
77 new_person = Person(name=input.name, phone_number=input.phone_number)
79 # errors.append('There was an error saving to the database')
82 render('success.tmpl', locals())
84 def validate_phone(self, phone_number):
86 if len(phone_number) < 7:
87 errors.append('Your phone number is too short')
89 people = store.find(Person)
91 if phone_number[-7:] == person.phone_number[-7:]:
92 errors.append('That phone number already exists in our datbase.')
99 people = store.find(Person)
100 render('show_names.tmpl', locals())
105 questionaires = store.find(Questionaire)
106 render('questionaires.tmpl', locals())
109 questionaires = store.find(Questionaire)
114 errors.append('You must enter a code.')
117 new_questionaire = Questionaire(code=input.code,
118 description=input.description)
119 store.add(new_questionaire)
122 render('questionaires.tmpl', locals())
124 class edit_questions:
127 questionaire = store.get(Questionaire, int(id))
128 render('edit_questions.tmpl', locals())
132 questionaire = store.get(Questionaire, int(id))
135 if not input.longdesc:
136 errors.append('You must have a long description.')
138 code = self.__get_code(questionaire)
141 new_question = Question(code=code,
142 shortdesc=input.shortdesc,
143 datatype=input.datatype,
144 longdesc=input.longdesc,
145 questionaire_id=questionaire.id)
146 store.add(new_question)
149 render('edit_questions.tmpl', locals())
151 def __get_code(self, questionaire):
152 """Return a new code that is higher than the others."""
154 codes = list(int(q.code) for q in questionaire.questions)
155 return(len(codes) + 1)
162 render('test_sent.tmpl')
166 #TODO magic to parse sms
170 web.webapi.internalerror = web.debugerror
171 if __name__ == "__main__":
172 web.run(urls, globals(), web.reloader)
174 application = web.wsgifunc(web.webpyfunc(urls, globals()))