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 signup(request): """ create new user """ if request.method == "POST": tc = templates.Template() udb = db.Users() # username username = request.POST["username"] username = to_unicode(username.strip()) user = udb.get_by_username(username) if user: data = { "message": "Username already exists" } html = unicode(tc.make_start_template(request, data)) return HttpResponse(html) # email email = request.POST["email"] email = to_unicode(email.lower().strip()) user = udb.get_by_email(email) if user: data = { "message": "This email is already registered" } html = unicode(tc.make_start_template(request, data)) return HttpResponse(html) # real name realname = request.POST["realname"] realname = to_unicode(realname.strip()) # get the password and validate it password = request.POST["password"] password = to_unicode(password.strip()) password = crypt.convert_to_md5(password) # create user userid = udb.insert(realname, username, email, password) if not userid: html = "OOPS!" return HttpResponse(html) # make url: serialized = crypt.encrypt_userid(str(userid)) url = unicode("/welcome/" + serialized) # redirect to Welcome URL return redirect(url)
def post(self): tc = templates.Template() udb = db.Users() # username username = self.get_argument("username") username = to_unicode(username.strip()) user = udb.get_by_username(username) if user: data = { "message": "Username already exists" } html = unicode(tc.make_start_template(data)) self.write(html) return # email email = self.get_argument("email") email = to_unicode(email.lower().strip()) user = udb.get_by_email(email) if user: data = { "message": "This email is already registered" } html = unicode(tc.make_start_template(data)) self.write(html) return # real name realname = self.get_argument("realname") realname = to_unicode(realname.strip()) # get the password and validate it password = self.get_argument("password") password = to_unicode(password.strip()) password = crypt.convert_to_md5(password) # create user userid = udb.insert(realname, username, email, password) if not userid: self.write("OOPS!!") return # make url: serialized = crypt.encrypt_userid(str(userid)) url = unicode("/welcome/" + serialized) # redirect to Welcome URL 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 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 process_forms(args): """ take args from POST request (tasting_notes) and build a dictionary Input: args: arguments from POST request userid: unicode - uuid Output: wine_data - dict 'name' - unicode 'typed' - unicode 'pics' - json 'general_info' - json 'fruit_family' - json 'fruit_quality' - json 'non_fruit_quality' - json 'structure' - json 'notes' - unicode """ # Name of Wine name = u"" if 'name' in args: name = to_unicode(args['name']) name = name.lower() # Address where the pictures are stored in the server pics = args['pics'] # notes notes = args['notes'] notes = to_unicode(notes) # Fruit quality fruit_quality = {} try: fruit_quality['tart'] = args['tart'] except: fruit_quality['tart'] = '0' try: fruit_quality['ripe'] = args['ripe'] except: fruit_quality['ripe'] = '0' try: fruit_quality['overripe'] = args['overripe'] except: fruit_quality['overripe'] = '0' try: fruit_quality['baked'] = args['baked'] except: fruit_quality['baked'] = '0' # General info: general_info = {} try: # 1 - cool/moderate # 2 - warm/hot general_info['climate'] = args['climate'] except: general_info['climate'] = '0' try: # 1 - new world # 2 - old world general_info['style'] = args['style'] except: general_info['style'] = '0' try: # 1 - 1-3 years # 2 - 4-6 years # 3 - 7+ years general_info['age'] = args['age'] except: general_info['age'] = '0' try: general_info['country'] = to_unicode(args['country']) except: general_info['country'] = u"" typed = args['type'] if typed == 'red': try: # 1 - Cavernet Sauv or Merlot # 2 - Syrah/shiraz # 3 - pinot noir o rgamay # 4 - tempranillo or grenache # 5 - sangiovese or nebbiolo # 6 - malbec or zinfandel general_info['grape'] = args['grape'] except: general_info['grape'] = '0' try: # 1 - garnet # 2 - ruby # 3 - purple general_info['color'] = args['color'] except: general_info['color'] = '0' # Fruit Family fruit_family = {} try: fruit_family['red'] = args['red'] except: fruit_family['red'] = '0' try: fruit_family['black'] = args['black'] except: fruit_family['black'] = '0' try: fruit_family['blue'] = args['blue'] except: fruit_family['blue'] = '0' try: fruit_family['fig_raisin'] = args['fig_raisin'] except: fruit_family['fig_raisin'] = '0' # Non-Fruit quality nonfruit_quality = {} try: nonfruit_quality['flowers'] = args['flowers'] except: nonfruit_quality['flowers'] = '0' try: nonfruit_quality['vegetal'] = args['vegetal'] except: nonfruit_quality['vegetal'] = '0' try: nonfruit_quality['herbs'] = args['herbs'] except: nonfruit_quality['herbs'] = '0' try: nonfruit_quality['peppercorn'] = args['peppercorn'] except: nonfruit_quality['peppercorn'] = '0' try: nonfruit_quality['vanilla'] = args['vanilla'] except: nonfruit_quality['vanilla'] = '0' try: nonfruit_quality['game'] = args['game'] except: nonfruit_quality['game'] = '0' try: nonfruit_quality['balsamic'] = args['balsamic'] except: nonfruit_quality['balsamic'] = '0' try: nonfruit_quality['organic_earth'] = args['organic_earth'] except: nonfruit_quality['organic_earth'] = '0' try: nonfruit_quality['inorganic_earth'] = args['inorganic_earth'] except: nonfruit_quality['inorganic_earth'] = '0' try: nonfruit_quality['oak'] = args['oak'] except: nonfruit_quality['oak'] = '0' # Structure structure = {} try: # 1. dry # 2. off dry # 3. sweet structure['sweet'] = args['sweet'] except: structure['sweet'] = '0' try: structure['tannin'] = args['tannin'] except: structure['tannin'] = '0' try: structure['acid'] = args['acid'] except: structure['acid'] = '0' try: structure['alcohol'] = args['alcohol'] except: structure['alcohol'] = '0' # white wine else: try: # 1 - Chenin Blanc or Pinot Gris # 2 - Pinot Grigio # 3 - Riesling or Albarino # 4 - Chardonnay # 5 - Gewurz or Torrontes # 6 - Sauvignon Blanc general_info['grape'] = args['grape'] except: general_info['grape'] = '0' try: # 1 - straw # 2 - yellow # 3 - gold general_info['color'] = args['color'] except: general_info['color'] = '0' # Fruit Family fruit_family = {} try: fruit_family['apple_pear'] = args['apple_pear'] except: fruit_family['apple_pear'] = '0' try: fruit_family['citrus'] = args['citrus'] except: fruit_family['citrus'] = '0' try: fruit_family['pitted_stone'] = args['pitted_stone'] except: fruit_family['pitted_stone'] = '0' try: fruit_family['tropical'] = args['tropical'] except: fruit_family['tropical'] = '0' try: fruit_family['melon'] = args['melon'] except: fruit_family['melon'] = '0' # Non-Fruit quality nonfruit_quality = {} try: nonfruit_quality['flowers'] = args['flowers'] except: nonfruit_quality['flowers'] = '0' try: nonfruit_quality['vegetal'] = args['vegetal'] except: nonfruit_quality['vegetal'] = '0' try: nonfruit_quality['ginger'] = args['ginger'] except: nonfruit_quality['ginger'] = '0' try: nonfruit_quality['vanilla'] = args['vanilla'] except: nonfruit_quality['vanilla'] = '0' try: nonfruit_quality['yeast'] = args['yeast'] except: nonfruit_quality['yeast'] = '0' try: nonfruit_quality['butter'] = args['butter'] except: nonfruit_quality['butter'] = '0' try: nonfruit_quality['organic_earth'] = args['organic_earth'] except: nonfruit_quality['organic_earth'] = '0' try: nonfruit_quality['inorganic_earth'] = args['inorganic_earth'] except: nonfruit_quality['inorganic_earth'] = '0' try: nonfruit_quality['oak'] = args['oak'] except: nonfruit_quality['oak'] = '0' # Structure structure = {} try: # 1. dry # 2. off dry # 3. sweet structure['sweet'] = args['sweet'] except: structure['sweet'] = '0' try: structure['bitter_phenolic'] = args['bitter_phenolic'] except: structure['bitter_phenolic'] = '0' try: structure['acid'] = args['acid'] except: structure['acid'] = '0' try: structure['alcohol'] = args['alcohol'] except: structure['alcohol'] = '0' wine_data = { 'name': name, 'typed': typed, 'pics': pics, 'general_info': general_info, 'fruit_family': fruit_family, 'fruit_quality': fruit_quality, 'non_fruit_quality': nonfruit_quality, 'structure': structure, 'notes': notes, } return wine_data