def login(): """ User login page. GET: Serve Log-in page. POST: If form is valid and new user creation succeeds, redirect user to the logged-in homepage. """ if current_user.is_authenticated: return redirect('/api/admin') # Bypass if user is logged in login_form = LoginForm() if request.method == 'POST': if login_form.validate_on_submit(): email = login_form.data.get('email') password = login_form.data.get('password') user = db_session.query(User).filter( User.email == email).first() # Validate Login Attempt if user and user.check_password(password=password): login_user(user) next_page = request.args.get('next') return redirect(next_page or '/api/admin') flash('Invalid username/password combination') return redirect('/api/login') return render_template('login.html', form=login_form, title='Log in.', template='login-page', body="Log in with your User account.")
def get(self): current_cart = session.get("cart", {}) products = db_session.query(RetailProduct).join(Product).filter( RetailProduct.id.in_(current_cart.keys())) id_to_prod = {} cart_items = [] for product in products: id_to_prod[product.id] = product grand_total = 0 for prod_id, qty in current_cart.items(): retail_prod = id_to_prod[prod_id] price = (decimal.Decimal(retail_prod.price) / decimal.Decimal("100.0")).quantize( decimal.Decimal('0.00')) cart_items.append({ "name": retail_prod.name, "price": '${}'.format(price), "qty": qty, "total": '${}'.format(price * qty) }) grand_total += price * qty return { 'cart': cart_items, 'user': session.get('user'), 'count': len(current_cart), 'total': '${}'.format(grand_total) }
def create_charge(): post_data = request.get_json() amount = round(float(post_data.get('total').strip("$")) * 100) current_cart = session.get("cart", {}) products = db_session.query(RetailProduct).join(Product).filter( RetailProduct.id.in_(current_cart.keys())) id_to_prod = {} distributor_id = None for product in products: if not distributor_id: distributor_id = product.distributor_id id_to_prod[product.id] = product grand_total = 0 for prod_id, qty in current_cart.items(): retail_prod = id_to_prod[prod_id] price = retail_prod.price grand_total += price * qty if grand_total != amount: return {"something went wrong"}, 400 stripe.api_key = 'sk_test_ZIhkqTdUPX3vIAjGbvgKSBj900rIS9blAJ' charge = stripe.Charge.create(amount=amount, currency='usd', card=post_data.get('token'), description="order") response_object = {'status': 'success', 'charge': charge} user = session["user"] if charge.status == 'succeeded': order = Order(distributor_id=distributor_id, submitted_at=datetime.datetime.now(), payed_at=datetime.datetime.now(), name=user['fllname'], email=user['email'], zip=user['zipcode'], address='1 main', city='Spokane', state='WA', phone='44455566') db_session.add(order) db_session.commit() for prod_id, qty in current_cart.items(): retail_prod = id_to_prod[prod_id] price = retail_prod.price oi = OrderItem(order_id=order.id, purchase_price=price, qty=qty, retail_product_id=prod_id) db_session.add(oi) db_session.commit() session["cart"] = {} return jsonify(response_object), 200
def post(self): zipcode = request.json['reg']['zipcode'] zip = db_session.query(Zip).filter(Zip.zip_code == zipcode).first() if not zip: return {}, 404 session["user"] = request.json['reg'] resp = {'distributor': zip.distributor_id} return resp, 201
def get_class_features(city_id): """ Query db for class features related to a cunique city id """ class_features = db_session.query( base_classes["class_feature"]).filter( base_classes["class_feature"].resp_city == city_id).all() return class_features
def get(self, id): product = db_session.query(RetailProduct).filter( RetailProduct.id == id).first() if not product: return "Not found", 404 return { 'product': product_schema.dump(product), 'user': session.get('user'), 'count': len(session.get("cart", {})) }
def can_order(self): if self.distributor_daily_limit is None: return True # check if order limit for distributor reached from session import db_session total_orders = db_session.query( func.count(OrderItem.qty).label('total')).join(Order).filter( OrderItem.retail_product_id == self.id).filter( Order.created_at >= datetime.date.today()).first()[0] if total_orders >= self.distributor_daily_limit: return False return True
def get(self, distributor_id): args = request.args products = db_session.query(RetailProduct).join(Product).filter( RetailProduct.distributor_id == distributor_id) if args.get("cat") and args.get("cat") != 'All': products = products.filter( Product.product_category == args.get("cat")) return { 'products': products_schema.dump(products), 'user': session.get('user'), 'count': len(session.get("cart", {})) }
class ImageFileForm(ModelForm): class Meta: model = base_classes["image_file"] respective_city = QuerySelectField("Respective city", query_factory=get_cities, get_label='full_name', id='respective_city') respective_feature_choices = [ (row.id, row.name) for row in db_session.query(base_classes["class_feature"]).all() ] respective_feature = SelectField('Respective feature:', choices=respective_feature_choices, coerce=int, validators=[DataRequired()], id='respective_feature') image_file_location = StringField( 'image file location', validators=[DataRequired(), validate_file_exists]) submit = SubmitField() def get_image_file_footprint_data(self): """ get footprint and projection of an image file contained within city footprint """ # Treat raster case if dict(self.respective_feature_choices).get( self.respective_feature.data) == "raw": raster = get_raster_from_s3(self.image_file_location.data) city_footprint, city_projection = db_session.query( base_classes["cities"].footprint, base_classes["cities"].footprint_wkt_proj).filter( base_classes["cities"].id == int( self.respective_city.data.id)).all()[0] city_geom = wkt.loads(city_footprint) city_geom_reprojected = reproject_geom( city_geom, str(raster.meta["crs"]).lower(), city_projection.lower()) raster_bbox = box(*raster.bounds) geoms_intersection = city_geom_reprojected.intersection( raster_bbox) return geoms_intersection.wkt
def get_image_file_footprint_data(self): """ get footprint and projection of an image file contained within city footprint """ # Treat raster case if dict(self.respective_feature_choices).get( self.respective_feature.data) == "raw": raster = get_raster_from_s3(self.image_file_location.data) city_footprint, city_projection = db_session.query( base_classes["cities"].footprint, base_classes["cities"].footprint_wkt_proj).filter( base_classes["cities"].id == int( self.respective_city.data.id)).all()[0] city_geom = wkt.loads(city_footprint) city_geom_reprojected = reproject_geom( city_geom, str(raster.meta["crs"]).lower(), city_projection.lower()) raster_bbox = box(*raster.bounds) geoms_intersection = city_geom_reprojected.intersection( raster_bbox) return geoms_intersection.wkt
def find_by_type(self, type): return db_session.query(Collection).filter_by(type=type)
def find_by_team_id(self, team_id): return db_session.query(Collection).filter_by(team_id=team_id)
def find_by_user_id(self, user_id): return db_session.query(Profile).filter_by(user_id=user_id)
def find_all(self): return db_session.query(Profile)
def find_by_channel_id(self, channel_id): return db_session.query(Font).filter_by(channel_id=channel_id)
def find_all_installed(self): return db_session.query(Font).filter_by(is_installed=True)
def find_all_font_ids(self): return db_session.query(Font.font_id)
def find_all(self): return db_session.query(Font).all()
def find_all_chosen(self): return db_session.query(Font).filter_by(is_chosen=True)
def find_subscription(self, font_id, user_id): return db_session.query(Subscription).filter_by( font_id=font_id, user_id=user_id )
def find_all_installable(self): return db_session.query(Font).filter_by(is_installed=False)
def find_by_tag(self, tag): return db_session.query(FontType).filter_by(tag=tag)
def find_all_upgradable(self): return db_session.query(Font).filter_by(is_upgradable=True)
def find_by_type_id(self, type_id): return db_session.query(FontType).filter_by(type_id=type_id)
def find_by_font_id(self, font_id): return db_session.query(Font).filter_by(font_id=font_id)
def find_all(self): return db_session.query(Collection).all()
def find_by_email(self, email): return db_session.query(Profile).filter_by(email=email).first()
def find_all_collection_ids(self): return db_session.query(Collection.collection_id)
def find_logged_user(self): return db_session.query(Profile).filter_by(is_logged=True).first()
def find_by_collection_id(self, collection_id): return db_session.query(Collection).filter_by( collection_id=collection_id)