async def homepage(request): try: filter = request.query_params['filter'] if filter == 'under-100': data = request.state.db.listingsAndReviews.find( { '$and': [{ 'cleaning_fee': { '$exists': True } }, { 'price': { '$lt': 100 } }] }, limit=15) elif filter == 'highly-rated': data = request.state.db.listingsAndReviews.find( { '$and': [{ 'cleaning_fee': { '$exists': True } }, { 'price': { '$lt': 100 } }, { 'review_scores.review_scores_rating': { '$gt': 90 } }] }, limit=15) elif filter == 'surprise': data = request.state.db.listingsAndReviews.find( { 'cleaning_fee': { '$exists': True }, 'amenities': { '$in': ["Pets allowed", "Patio or balcony", "Self check-in"] } }, limit=15) except KeyError: data = request.state.db.listingsAndReviews.find( {'cleaning_fee': { '$exists': True }}, limit=15) response = [] for doc in data: response.append( Property(doc['_id'], doc['name'], doc['summary'], doc['address']['street'], str(doc['price']), str(doc['cleaning_fee']), str(doc['accommodates']), doc['images']['picture_url'], doc['amenities'])) return templates.TemplateResponse('index.html', { 'request': request, 'response': response })
except: return 0 def tryfloat(val): try: return float(val) except: return float(0) for inx, row in enumerate(csvreader): row = map(lambda x: x.decode('utf-8'), row) prop = Property() prop.headline = '' # db.StringProperty() #no prop.main_description = row[cnames.index( "PROP_DescripcionGeneral")] + ' ' + row[ cnames.index("PROP_DireccionOtros" )] # db.StringProperty() #prop_descripciongeneral #print type(row[cnames.index("PROP_PROV_Id")]) prop.country = u'Argentina' # Argentina prop.state = row[cnames.index("PROP_PROV_Id")] # PRop_prov_id prop.city = row[cnames.index("PROP_LOCAL_Id")] # prov local id prop.neighborhood = row[cnames.index("PROP_barrio")] prop.street_name = row[cnames.index("PROP_Calle")] prop.street_number = int(tryint(row[cnames.index("PROP_Nro")]))
def add_device_api(): body = request.get_json() if 'tag' not in body or 'name' not in body: error = {"Error": "No mandatory property is set."} return make_response(jsonify(error), 400) else: tag = body['tag'] name = body['name'] description = body.get('description', None) device_parent = body.get('device_parent', None) is_gateway = body.get('is_gateway', None) ipv4_address = body.get('ipv4_address', None) properties = body.get('properties', None) resources = body.get('resources', None) devices = Device.query.filter_by(tag=tag).first() if devices is not None: error = {"Error": "The device with this tag is already exist."} return make_response(jsonify(error), 400) if device_parent is not None: device = Device.query.filter_by(id=device_parent) if device is None: error = {"Error": "The parent device ID not exist."} return make_response(jsonify(error), 400) if properties: for proper in properties: keys_list = list(proper.keys()) if "name" not in keys_list or "value" not in keys_list: error = { "Error": "The properties doesn't have the mandatory attributes." } return make_response(jsonify(error), 400) if resources: for resource in resources: keys_list = list(resource.keys()) if "tag" not in keys_list or "name" not in keys_list or "resource_type" not in keys_list: error = { "Error": "The resources doesn't have the mandatory attributes." } return make_response(jsonify(error), 400) device = Device(tag=tag, name=name, description=description, ipv4_address=ipv4_address, is_gateway=is_gateway, device_parent=device_parent) db.session.add(device) db.session.commit() if properties: for proper in properties: if "description" not in list(proper.keys()): proper_d = Property(name=proper["name"], value=proper["value"], device_id=device.id) else: proper_d = Property(name=proper["name"], value=proper["value"], description=proper["description"], device_id=device.id) db.session.add(proper_d) if resources: for resource in resources: if "description" not in list(resource.keys()): resource_d = Resource( tag=resource["tag"], name=resource["name"], resource_type=resource["resource_type"], device_id=device.id) else: resource_d = Resource( tag=resource["tag"], name=resource["name"], description=resource["description"], resource_type=resource["resource_type"], device_id=device.id) db.session.add(resource_d) db.session.commit() return jsonify(device.serialize)
def update_device_api(tag): device = Device.query.filter_by(tag=tag).first() if device is not None: body = request.get_json() propers = [] resources_list = [] #Get properties from request name = body.get('name', None) description = body.get('description', None) device_parent = body.get('device_parent', None) is_gateway = body.get('is_gateway', None) ipv4_address = body.get('ipv4_address', None) properties = body.get('properties', None) resources = body.get('resources', None) #Set properties to model if name: device.name = name if description: device.description = description if device_parent: device.device_parent = device_parent if is_gateway is not None: device.is_gateway = is_gateway if ipv4_address: device.ipv4_address = ipv4_address if properties: for proper in properties: keys_list = list(proper.keys()) if "name" not in keys_list or "value" not in keys_list: error = { "Error": "The properties doesn't have the mandatory attributes." } return make_response(jsonify(error), 400) properti = Property(name=proper["name"], value=proper["value"], description=proper.get( "description", None)) propers.append(properti) if resources: for resource in resources: keys_list = list(resource.keys()) if "tag" not in keys_list or "name" not in keys_list or "resource_type" not in keys_list: error = { "Error": "The resources doesn't have the mandatory attributes." } return make_response(jsonify(error), 400) resource_i = Resource(tag=resource["tag"], name=resource.get("name", None), resource_type=resource.get( "resource_type", None)) resources_list.append(resource_i) db.session.add(device) db.session.commit() if len(propers) > 0: for new_proper in propers: proper = Property.query.filter_by( device_id=device.id, name=new_proper.name).first() if proper is not None: proper.value = new_proper.value proper.description = new_proper.description db.session.add(proper) else: new_proper.device_id = device.id db.session.add(new_proper) db.session.commit() if len(resources_list) > 0: for new_resource in resources_list: resource = Resource.query.filter_by( device_id=device.id, tag=new_resource.tag).first() if resource is not None: print("entro aquĆ") resource.name = new_resource.name resource.resource_type = new_resource.resource_type if new_resource.description: resource.description = new_resource.description db.session.add(resource) else: print("entro aca") new_resource.device_id = device.id db.session.add(new_resource) db.session.commit() return jsonify(device.serialize) else: error = {"Error": "The device doesn't exist."} return make_response(jsonify(error), 400)
def extract(): if 'email' not in session: return redirect(url_for('login')) option_list = get_properties() option_list2 = list(option_list) listofchoices = list(zip(option_list, option_list2)) propertyform = ExtractForm() propertyform.search_property.choices = listofchoices useremail = session['email'] propertyindb = Property.query.with_entities( Property.search_property, Property.brand_queries).filter_by(email=useremail).all() if request.method == 'POST': if propertyform.validate() == False: flash('Form validation not passed') return render_template('extract.html', form=propertyform, option_list=option_list) else: # get the project and API key email = session['email'] propertyindb = Property.query.filter_by( search_property=propertyform.search_property.data).first() if propertyindb is None: # save the keys into database newautho = Property(propertyform.search_property.data, propertyform.brand_queries.data, email) db.session.add(newautho) db.session.commit() else: propertyindb.brand_queries = propertyform.brand_queries.data db.session.commit() # get the web property, start and end dates from form property_uri = propertyform.search_property.data start_date = propertyform.start_date.data end_date = propertyform.end_date.data # generate a request to searchanalytics to extract data query_response = generate_request(property_uri, start_date, end_date) if query_response: flash(query_response) propertyindb = Property.query.with_entities( Property.search_property, Property.brand_queries).filter_by(email=useremail).all() # return those results return render_template('extract.html', form=propertyform, option_list=option_list, query_response=query_response, branded_queries=propertyindb) elif request.method == 'GET': return render_template("extract.html", form=propertyform, option_list=option_list, branded_queries=propertyindb)