Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
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)