def get(self, encrypted_userid, wineid): # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: self.write("OOPS!") return # delete the pictures wdb = db.Wines() wine = wdb.get_by_wineid(wineid) if not wine: self.write("no wine") path = "/home/ydelgado/sommelier_app/" if wine['pics']['front_label']: pic_path = path + wine['pics']['front_label'] os.remove(pic_path) if wine['pics']['back_label']: pic_path = path + wine['pics']['back_label'] os.remove(pic_path) # delete wine from database res = wdb.delete_by_wineid(wineid) if not res: print "ERROR: ", wineid # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) self.redirect(url) return # redirect to Old Tasting Notes URL url = unicode("/old_notes/" + encrypted_userid) self.redirect(url)
def get(self, encrypted_userid): # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: self.write("OOPS!") return # get the list of stores wines wdb = db.Wines() wines = wdb.get_by_userid(userid) if not wines: # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) self.redirect(url) return wines = [{ 'name': x['name'].title(), 'wineid': x['wineid'] } for x in wines] templatedata = { 'wines': wines, 'show_tasting_note': False, 'welcome_url': unicode("/welcome/" + encrypted_userid), 'color': "#990000;", 'post_url': unicode("/old_notes/" + encrypted_userid) } # check if delete button is redirecting here print "referer", self.request.headers.get('Referer') if 'old_notes' in self.request.headers.get('Referer'): templatedata['message'] = "The entry was deleted succesfully." tc = templates.Template() html = to_unicode(tc.make_old_tastingnotes_template(templatedata)) self.write(html)
def post(self, encrypted_userid, wine_type): # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: self.write("OOPS!") return udb = db.Users() user = udb.get_by_userid(userid) if not userid: self.write("OOPS!") return # store both pictures in the data base try: file_pic = self.request.files["front_label"] file_pic = file_pic[0] original_fname = file_pic['filename'] extension = os.path.splitext(original_fname)[1] filename_front = "data/uploads/" + str(uuid.uuid4()) + extension output_file = open(filename_front, 'wb') output_file.write(file_pic['body']) print "file " + filename_front + " is uploaded." except: filename_front = "" # store both pictures in the data base try: file_pic = self.request.files["back_label"] file_pic = file_pic[0] original_fname = file_pic['filename'] extension = os.path.splitext(original_fname)[1] filename_back = "data/uploads/" + str(uuid.uuid4()) + extension output_file = open(filename_back, 'wb') output_file.write(file_pic['body']) print "file " + filename_back + " is uploaded." except: filename_back = "" pics = { 'front_label': to_unicode(filename_front), 'back_label': to_unicode(filename_back) } # process all the elements from the forms args = { k: self.get_argument(k) for k in self.request.arguments } args['pics'] = pics if 'red' in wine_type: args['type'] = 'red' else: args['type'] = 'white' res = forms_util.store_wine_data(args, userid) if not res: self.write("OOPS") return # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) self.redirect(url)
def post(self, encrypted_userid): # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: self.write("OOPS!") return udb = db.Users() user = udb.get_by_userid(userid) if not userid: self.write("OOPS!") return wine_type = self.get_argument("wine_type") if not wine_type: self.write("ERROR: wine_type") return # make tasting notes URL url = '/notes/' + encrypted_userid + '/tasting_notes_' + wine_type url = unicode(url) data = { 'url': url, 'username': unicode(user['username'].title()), } tc = templates.Template() html = unicode(tc.make_tastingnotes_template(wine_type, data)) self.write(html)
def get(self, encrypted_userid): # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: self.write("OOPS!") return udb = db.Users() user = udb.get_by_userid(userid) if not userid: self.write("OOPS!") return # check if this is the first time that a user # visits this page phrase = u"How are you doing? Glad to have you here again." if encrypted_userid in self.request.headers.get('Referer'): phrase = u"Your tasting notes were successfully saved." # url where user can see older tasting notes url_old_tasting_notes = "/old_notes/" + encrypted_userid template_data = { 'name': unicode(user['real_name'].title()), 'welcome_text': phrase, 'url_old_tasting_notes': url_old_tasting_notes, } tc = templates.Template() html = unicode(tc.make_welcome_template(template_data)) self.write(html)
def post(self, encrypted_userid): # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: self.write("OOPS!") return # get the selected wine wineid = self.get_argument("name") if not wineid: # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) self.redirect(url) return # get the list of stored wines wdb = db.Wines() wines = wdb.get_by_userid(userid) if not wines: # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) self.redirect(url) return # pick the selected wine wine = [x for x in wines if x['wineid']==wineid] if not wine: print "ERROR: ", wineid # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) self.redirect(url) return # list of wines for <select> wines = [{ 'name': x['name'].title(), 'wineid': x['wineid'] } for x in wines] wine = wine[0] if wine['typed'] == "red": color = "#990000;" else: color = "#99CC33;" templatedata = forms_util.compose_wine_data_for_template(wine) templatedata['wines'] = wines templatedata['show_tasting_note'] = True templatedata['welcome_url'] = unicode("/welcome/" + encrypted_userid) templatedata['color'] = color templatedata['post_url'] = unicode("/old_notes/" + encrypted_userid) templatedata['delete_url'] = unicode("/delete/" + encrypted_userid + "/" + wineid) tc = templates.Template() html = unicode(tc.make_old_tastingnotes_template(templatedata)) self.write(html)
def delete_wine(request, encrypted_userid, wineid): """ delete wine with wineid from the DB and also the pictures from the /uploads/ folder. """ if request.method == "GET": # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: html = "OOPS!" return HttpResponse(html) # delete the pictures wdb = db.Wines() wine = wdb.get_by_wineid(wineid) if not wine: html = "no wine!" return HttpResponse(html) if wine['pics']['front_label']: pic_path = os.path.join(BASE_DIR, wine['pics']['front_label']) os.remove(pic_path) if wine['pics']['back_label']: pic_path = os.path.join(BASE_DIR, wine['pics']['back_label']) os.remove(pic_path) # delete wine from database res = wdb.delete_by_wineid(wineid) if not res: print "ERROR: ", wineid # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) return redirect(url) # redirect to Old Tasting Notes URL url = unicode("/old_notes/" + encrypted_userid) return redirect(url)
def old_notes(request, encrypted_userid): """ Template where stored tasting notes are shown """ if request.method == "GET": # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: html = "OOPS!" return HttpResponse(html) # get the list of stores wines wdb = db.Wines() wines = wdb.get_by_userid(userid) if not wines: # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) return redirect(url) wines = [{ 'name': x['name'].title(), 'wineid': x['wineid'] } for x in wines] templatedata = { 'wines': wines, 'show_tasting_note': False, 'welcome_url': unicode("/welcome/" + encrypted_userid), 'color': "#990000;", 'post_url': unicode("/old_notes/" + encrypted_userid) } # check if delete button is redirecting here if 'old_notes' in request.META['HTTP_REFERER']: templatedata['message'] = "The entry was deleted succesfully." tc = templates.Template() html = to_unicode(tc.make_old_tastingnotes_template(request, templatedata)) return HttpResponse(html) elif request.method == "POST": # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: html = "OOPS!" return HttpResponse(html) # get the selected wine wineid = request.POST["name"] if not wineid: # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) return redirect(url) # get the list of stored wines wdb = db.Wines() wines = wdb.get_by_userid(userid) if not wines: # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) return redirect(url) # pick the selected wine wine = [x for x in wines if x['wineid']==wineid] if not wine: print "ERROR: ", wineid # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) return redirect(url) # list of wines for <select> wines = [{ 'name': x['name'].title(), 'wineid': x['wineid'] } for x in wines] wine = wine[0] if wine['typed'] == "red": color = "#990000;" else: color = "#99CC33;" templatedata = forms_util.compose_wine_data_for_template(wine) templatedata['wines'] = wines templatedata['show_tasting_note'] = True templatedata['welcome_url'] = unicode("/welcome/" + encrypted_userid) templatedata['color'] = color templatedata['post_url'] = unicode("/old_notes/" + encrypted_userid) templatedata['delete_url'] = unicode("/delete/" + encrypted_userid + "/" + wineid) tc = templates.Template() html = unicode(tc.make_old_tastingnotes_template(request, templatedata)) return HttpResponse(html)
def new_tasting_note(request, encrypted_userid, wine_type): """ red wine or white wine tasting notes form. """ if request.method == "POST": # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: html = "OOPS!" return HttpResponse(html) udb = db.Users() user = udb.get_by_userid(userid) if not userid: html = "OOPS!" return HttpResponse(html) # store both pictures in the data base try: front_pic = request.FILES["front_label"] extension = os.path.splitext(front_pic.name)[1] front_pic_name = "uploads/" + str(uuid.uuid4()) + extension pic_name = os.path.join(BASE_DIR, front_pic_name) try: output_file = open(pic_name, 'wb') except Exception as e: print e output_file.write(front_pic.read()) print "file " + front_pic_name + " is uploaded." except: front_pic_name = "" # store both pictures in the data base try: back_pic = request.FILES["back_label"] extension = os.path.splitext(back_pic.name)[1] back_pic_name = "uploads/" + str(uuid.uuid4()) + extension pic_name = os.path.join(BASE_DIR, back_pic_name) output_file = open(pic_name, 'wb') output_file.write(back_pic.read()) except: back_pic_name = "" pics = { 'front_label': to_unicode(front_pic_name), 'back_label': to_unicode(back_pic_name) } # process all the elements from the forms args = request.POST args['pics'] = pics if 'red' in wine_type: args['type'] = 'red' else: args['type'] = 'white' res = forms_util.store_wine_data(args, userid) if not res: html = "OOPS!" return HttpResponse(html) # redirect to Welcome URL url = unicode("/welcome/" + encrypted_userid) return redirect(url)
def welcome(request, encrypted_userid): """ show older tasting notes and create a new tasting note """ if request.method == "GET": # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: html = "OOPS!" return HttpResponse(html) udb = db.Users() user = udb.get_by_userid(userid) if not userid: html = "OOPS!" return HttpResponse(html) # check if this is the first time that a user # visits this page phrase = u"Welcome back!" if encrypted_userid in request.META['HTTP_REFERER']: phrase = u"Your tasting notes were successfully saved." # url where user can see older tasting notes url_old_tasting_notes = "/old_notes/" + encrypted_userid template_data = { 'name': unicode(user['real_name'].title()), 'welcome_text': phrase, 'url_old_tasting_notes': url_old_tasting_notes, } tc = templates.Template() html = unicode(tc.make_welcome_template(request, template_data)) return HttpResponse(html) elif request.method == "POST": # decrypt the userid userid = crypt.decrypt_userid(encrypted_userid) if not userid: html = "OOPS!" return HttpResponse(html) udb = db.Users() user = udb.get_by_userid(userid) if not userid: html = "OOPS!" return HttpResponse(html) wine_type = request.POST["wine_type"] if not wine_type: html = "ERROR: wine_type" return HttpResponse(html) # make tasting notes URL url = '/notes/' + encrypted_userid + '/tasting_notes_' + wine_type url = unicode(url) if wine_type == "red": color = "#990000" else: color = "#99CC33" data = { 'url': url, 'username': unicode(user['username'].title()), 'type': unicode(wine_type), 'color': unicode(color), } tc = templates.Template() html = unicode(tc.make_tastingnotes_template(request, data)) return HttpResponse(html)