From db17249a9b3595a091f11e39fe1c14f43e07c66e Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 27 Oct 2014 13:21:15 -0400 Subject: [PATCH] opportunity: Creation of a Meetup can now return to its page on error Signed-off-by: Lucas Bajolet --- contrib/opportunity/src/opportunity_controller.nit | 64 +++++++++++++++----- .../opportunity/src/templates/meetup_creation.nit | 59 +++++++++++++++--- 2 files changed, 98 insertions(+), 25 deletions(-) diff --git a/contrib/opportunity/src/opportunity_controller.nit b/contrib/opportunity/src/opportunity_controller.nit index 032b7d3..42df348 100644 --- a/contrib/opportunity/src/opportunity_controller.nit +++ b/contrib/opportunity/src/opportunity_controller.nit @@ -46,34 +46,66 @@ class OpportunityWelcome var get = request.get_args var rq = url.split("/") if rq.has("meetup_create") then + var ansset = new HashSet[String] + var ans_tmp = "answer_" + var cnt = 1 + loop + var anss = request.string_arg(ans_tmp + cnt.to_s) + cnt += 1 + if anss == null then break + if ansset.has(anss) then continue + ansset.add anss + end + var mname = request.string_arg("meetup_name") var mdate = request.string_arg("meetup_date") var mplace = request.string_arg("meetup_place") - if mname == null or mdate == null or mplace == null then return bad_req + if mname == null or mdate == null or mplace == null then + if mname == null then mname = "" + if mdate == null then mdate = "" + if mplace == null then mplace = "" + var rsp = new HttpResponse(200) + var meetpage = new MeetupCreationPage + var meet = new Meetup(mname, mdate, mplace) + meetpage.ans = ansset + meetpage.meet = meet + meetpage.error = "Name, Date and Place are mandatory fields." + rsp.body = meetpage.write_to_string + return rsp + + end var db = new OpportunityDB.open(db_path) var meet = new Meetup(mname, mdate, mplace) - if meet == null then + if ansset.is_empty then db.close - return bad_req + var rsp = new HttpResponse(200) + var meetpage = new MeetupCreationPage + meetpage.meet = meet + meetpage.error = "You need to input at least one answer." + rsp.body = meetpage.write_to_string + return rsp end - meet.commit(db) - var ans_tmp = "answer_" - var cnt = 1 - loop - var anss = request.string_arg(ans_tmp + cnt.to_s) - if anss == null then break - var ans = new Answer(anss) + if not meet.commit(db) then + db.close + var meetid = (mname + mdate + mplace).sha1_to_s + var rsp = new HttpResponse(200) + var meetpage = new MeetupCreationPage + meetpage.meet = meet + meetpage.ans = ansset + meetpage.error = """

Could not create Meetup.

+

Hmm, that's embarassing, there already seems to be a Meetup like yours here.

+

If this is not yours, please contact the mainainers of the website, you might have found a bug !

""" + rsp.body = meetpage.write_to_string + return rsp + end + for v in ansset do + var ans = new Answer(v) ans.meetup = meet ans.commit(db) - cnt += 1 end db.close var rsp = new HttpResponse(200) - if meet.id == "" then - rsp.body = (new MeetupCreationPage).write_to_string - else - rsp.body = (new MeetupConfirmation(meet)).write_to_string - end + rsp.body = (new MeetupConfirmation(meet)).write_to_string return rsp end if rq.has("new_meetup") then diff --git a/contrib/opportunity/src/templates/meetup_creation.nit b/contrib/opportunity/src/templates/meetup_creation.nit index 8cd364a..498b524 100644 --- a/contrib/opportunity/src/templates/meetup_creation.nit +++ b/contrib/opportunity/src/templates/meetup_creation.nit @@ -15,10 +15,17 @@ module meetup_creation import boilerplate +import opportunity_model class MeetupCreationPage super OpportunityPage + var meet: nullable Meetup = null is writable + + var error: nullable String = null is writable + + var ans: Set[String] = new HashSet[String] is writable + init do header.page_js = """ function new_answer(sender){ @@ -33,25 +40,57 @@ class MeetupCreationPage ch.last().after(''); } """ - body = """ + + end + + redef fun rendering do + # Do stuff with body before rendering + var bdy = new Template + + bdy.add "
" + + if error != null then + bdy.add "

" + bdy.add """" + end + + bdy.add """ -
-
+ """ + bdy.add """
- + - + - +

Answers

- - -
+""" + + if ans.is_empty then + bdy.add """ + """ + else + var cnt = 1 + for v in ans do + bdy.add """ + + + """ + cnt += 1 + end + end + + bdy.add """
@@ -61,6 +100,8 @@ class MeetupCreationPage
""" + body = bdy + super end end -- 1.7.9.5