def login(): fields = [] fields.append( Field(name="email", title="Email", the_type='email', identifier='email', placeholder="Email")) fields.append( Field(name='password', title="Password", the_type="password", identifier='password', placeholder='Password')) title = "Login " form = Form(fields=fields, title=title) if request.method == 'GET': return render_template('login.html', login_form=form) try: user = User.get_by_id(request.form['email']) if user: if user.password == request.form['password']: print "pop" return logged_in(user) else: form.error = "User or Password was Incorrect" return render_template('login.html', login_form=form) else: form.error = "User or Password was Incorrect" return render_template('login.html', login_form=form) except KeyError as err: form.error = "Email or Password Was Not Filled Out Correctly" return render_template('login.html', login_form=form)
def autoload_fields_by_row(entity, row, prefix=''): """ Autoloads entity fields from imported data row. entity: core.importhandler.Entity entity, where we need add fields and subentities. row: dict data, loaded from datasource. prefix: string prefix to be added to the name. """ def getjson(x): try: res = json.loads(x) except: return None return res from entities import Entity, Field for key, val in row.iteritems(): data_type = 'string' if key not in entity.fields: if isint(val): data_type = 'integer' elif isfloat(val): data_type = 'float' else: item_dict = getjson(val) if item_dict: entity.fields[key] = Field( { 'name': key, 'column': key, 'transform': 'json' }, entity) if key not in entity.nested_entities_field_ds: json_entity = Entity(dict(name=key, datasource=key)) autoload_fields_by_row(json_entity, item_dict, prefix='{0}.'.format(key)) entity.nested_entities_field_ds[key] = json_entity continue if prefix: field_config = { 'name': prefix + key, 'jsonpath': '$.{0}'.format('.'.join(key.split('-'))) } else: field_config = {'name': key, 'type': data_type, 'column': key} entity.fields[key] = Field(field_config, entity) entity.fields_loaded = True
def admin(): if not session.get("admin"): return login() if request.method == "POST": file = request.files.get("file", None) content_type = None photo = None if file: file_data = file.read() content_type = file.content_type # photo=images.Image(file_data) photo = images.resize(file_data, width=500, height=500) category = Category(id=request.form["name"].strip(), categoryID=request.form["name"], name=request.form["name"], photo=photo, photo_mimetype=content_type) category.put() fields = [] fields.append( Field(name="name", title="Category Name", the_type='text', identifier='name', placeholder="Category Name")) fields.append( Field(name='file', title="Upload Photo", the_type="file", placeholder="Upload Photo", identifier="file", tag="file")) title = "Create Category" form = Form(fields=fields, title=title) reviews = Review.query(Review.flagged == True) categories = Category.query() return render_template("admin_homepage.html", reviews=reviews, categories=categories, category_form=form)
def signup(): return login() fields = [] fields.append( Field(name="email", title="Email", the_type='email', identifier='email', placeholder="Email")) fields.append( Field(name="name", title="Name", the_type='name', identifier='name', placeholder="Name")) fields.append( Field(name='password', title="Password", the_type='password', identifier='password', placeholder='Password')) title = "Signup" form = Form(fields=fields, title=title) if request.method == 'GET': return render_template('signup.html', signup_form=form) try: exists = User.get_by_id(request.form['email']) if exists: form.error = "Email Taken" return render_template('signup.html', signup_form=form) else: user = User(email=request.form['email'], id=request.form['email'], password=request.form['password'], name=request.form['name']) user.put() return signed_up(user) except KeyError as err: form.error = "Email or Password Was Not Filled Out Correctly" return render_template('signup.html', signup_form=form)
def post_item(): if not session.get("logged_in"): return login() if request.method == "GET": fields = [] fields.append( Field(name="name", title="Name", the_type='text', identifier='name', placeholder="Item Name")) fields.append( Field(name='description', title="Description", the_type="textarea", identifier='description', placeholder='Descriptive Description', tag="textarea")) categories = Category.query() options = [] for category in categories: option = {} option["name"] = category.name option["value"] = category.categoryID options.append(option) fields.append( Field(name='category', title="Item Category", the_type="select", identifier="category", placeholder="Select Item Category", tag="select", options=options)) fields.append( Field(name='price', title="Price", the_type="number", identifier="price", placeholder="10.95", step=True)) fields.append( Field(name='file', title="Upload Photo", the_type="file", placeholder="Upload Photo", identifier="file", tag="file")) fields.append( Field(name='biddable', title="Allow offer amounts other than suggested price.", the_type="number", identifier="biddable", tag="checkbox")) title = "Post Item" form = Form(fields=fields, title=title, submit="Post") notifications = Notification.query( Notification.user == session["user_id"]).order(-Notification.time) return render_template("post_item.html", item_form=form, notifications=notifications) if request.method == "POST": name = request.form.get("name") description = request.form.get("description") category = request.form.get("category") price = float(request.form.get("price")) seller_id = session["user_id"] biddable = False if request.form.get("biddable"): biddable = True # print "nooo" # return json.dumps(request.files) file = request.files.get("file", None) # print "whaattt??" file_data = None content_type = None photo = None if file: file_data = file.read() photo = images.resize(file_data, width=500, height=500) content_type = file.content_type new_item = Item(name=name, description=description, category=category, price=price, seller_id=seller_id, seller_name=session["first_name"] + " " + session["last_name"], biddable=biddable, sold=False, best_offer=0.0, photo=photo, photo_mimetype=content_type) new_item.put() return my_items()
def browse_item(item_id): item_id = int(item_id) if not session.get("logged_in"): return login() if request.method == "GET": item = Item.get_by_id(item_id) category_id = item.category print item.name seller = User.get_by_id(item.seller_id) previous_offer = Offer.query(Offer.bidder == session["user_id"], Offer.item == item_id).get() was_previous_offer = False if previous_offer: was_previous_offer = True fields = [] fields.append( Field( name="message", title="Message For Seller", the_type='text', identifier='message', placeholder= "A short message for the seller. Perhaps, where you can meet or payment options.", tag="textarea")) if item.biddable: fields.append( Field(name='amount', title="Offer Amount", the_type="number", identifier="amount", placeholder="10.95", step=True)) title = "Make Offer" form = Form(fields=fields, title=title) tags = Item_Tag.query(Item_Tag.item == item_id) notifications = Notification.query( Notification.user == session["user_id"]).order(-Notification.time) return render_template("browse_item.html", item=item, category_id=category_id, bid_form=form, previous_offer=previous_offer, was_previous_offer=was_previous_offer, offer=previous_offer, notifications=notifications, tags=tags) if request.method == "POST": item = Item.get_by_id(item_id) if not item or item.sold: return page_was_not_found( "Sorry but the item you tried to bid on has been removed by the seller" ) category_id = item.category seller = User.get_by_id(item.seller_id) previous_offer = Offer.query(Offer.bidder == session["user_id"], Offer.item == item_id).get() if previous_offer: previous_offer.key.delete() amount = item.price if item.biddable: amount = float(request.form["amount"]) offer = Offer(bidder=session["user_id"], item=item_id, message=request.form["message"], amount=amount, bidder_name=session["first_name"] + " " + session["last_name"], accepted=False, confirmed=False, item_name=item.name) offer.put() if item.biddable: item.update_best_offer(amount) notification_body = "Offer made on " + item.name + "for $" + str( offer.amount) notification = Notification(user=item.seller_id, body=notification_body, ntype="item-offer", item=item.key.id(), item_category=item.category, noticed=False, link="/my_items/" + str(item.key.id())) notification.put() fields = [] fields.append( Field( name="message", title="Message For Seller", the_type='text', identifier='message', placeholder= "A short message for the seller. Perhaps, where you can meet or payment options.", tag="textarea")) if item.biddable: fields.append( Field(name='amount', title="Offer Amount", the_type="number", identifier="amount", placeholder="10.95", step=True)) title = "Make Offer" form = Form(fields=fields, title=title, submit="Make Offer") tags = Item_Tag.query(Item_Tag.item == item_id) notifications = Notification.query( Notification.user == session["user_id"]).order(-Notification.time) return render_template("browse_item.html", item=item, category_id=category_id, bid_form=form, offer=offer, was_previous_offer=True, notifications=notifications, tags=tags)
def my_item(item_id): if not session.get("logged_in"): return login() item_id = int(item_id) item = Item.get_by_id(item_id) if request.method == "POST": if item.seller_id != session["user_id"]: return "Uninformative Error", 500 if request.form.get("tags"): tags = request.form["tags"].split(" ") for tag in tags: if tag == "": continue tag = tag.strip().lower() exists = Tag.get_by_id(tag) if not exists: new_tag = Tag(id=tag, name=tag) new_tag.put() exists = Item_Tag.get_by_id(str(item_id) + tag) if not exists: new_item_tag = Item_Tag(id=str(item_id) + tag, item=item_id, tag=tag) new_item_tag.put() else: item.name = request.form.get("name") item.description = request.form.get("description") item.category = request.form.get("category") item.price = float(request.form.get("price")) item.biddable = False if request.form.get("biddable"): item.biddable = True item.put() offers = Offer.query(Offer.item == item_id).order(Offer.amount) fields = [] fields.append( Field(name="name", title="Name", the_type='text', identifier='name', placeholder="Item Name", value=item.name)) fields.append( Field(name='description', title="Description", the_type="textarea", identifier='description', placeholder='Descriptive Description', tag="textarea", value=item.description)) categories = Category.query() options = [] for category in categories: option = {} option["name"] = category.name option["value"] = category.categoryID options.append(option) fields.append( Field(name='category', title="Item Category", the_type="select", identifier="category", placeholder="Select Item Category", tag="select", options=options, value=item.category)) fields.append( Field(name='price', title="Price", the_type="number", identifier="price", placeholder="10.95", step=True, value=item.price)) fields.append( Field(name='biddable', title="Allow offer amounts other than suggested price.", the_type="number", identifier="biddable", tag="checkbox", value="checked" if item.biddable else "")) fields.append( Field(name='item_id', the_type="hidden", identifier="item_id", value=item.key.id(), hidden="hidden")) title = "Edit Item" form = Form(fields=fields, title=title) fields1 = [] fields1.append( Field(name="tags", title="Tags", the_type='text', identifier='name', placeholder="Enter descriptive words seperated by spaces.")) title1 = "" submit = "Add Tag" form1 = Form(fields=fields1, title=title1, submit=submit) tags = Item_Tag.query(Item_Tag.item == item_id) notifications = Notification.query( Notification.user == session["user_id"]).order(-Notification.time) return render_template("view_my_item.html", offers=offers, item=item, notifications=notifications, item_form=form, tag_form=form1, tags=tags)