def get_user_plants(): ''' Route which returns a list of all of the user's plants in json format ''' if 'email' not in session: abort(403) plants_list = [] with util.MongoConnect(): user = users.objects(email=session['email']) if str(user) == '[]': abort(401) user = user[0] # add all plants in each of the user's gardens for garden_id in user.gardens: garden = gardens.objects(id=garden_id) if garden != []: garden = garden[0] for plant_id in garden.plants: plant = user_plants.objects(id=plant_id) if plant != []: plant = plant[0] plants_list.append(plant.to_dict()) return jsonify(plants_list), 200
def unsubscribe(user_email: str): with util.MongoConnect(): for user in users.objects(): if user_email == user.email: user.unsubscribed = True user.save() return "Successfully unsubscribed.", 200 return "User not found.", 404
def send_all_task_lists(self): """ Generate and send task lists for all subscribed users. """ with util.MongoConnect(): usrs = users.objects() for usr in usrs: self.generate_and_send_task_list(usr) print("task list sent to {}.".format(usr.email))
def login(): if 'email' in session: # TODO: change based on function name return redirect(url_for('get_user_gardens')) if request.method == 'POST': with util.MongoConnect(): for user in users.objects(): if request.json['email'] == user.email: session['email'] = user['email'] # TODO: change based on function name return "", 200 return "", 200 # TODO: change this return return {}
def delete_plant_in_garden(garden_id: str, plant_id: str): if 'email' not in session: abort(403) if not is_valid_id(garden_id): abort(401) if not is_valid_id(plant_id): abort(401) with util.MongoConnect(): user = users.objects(email=session['email']) if str(user) == '[]': abort(401) user = user[0] # need to do str(i) because user.gardens is a list of ObjectIdFields if garden_id in [str(i) for i in user.gardens]: try: garden = gardens.objects(id=garden_id) except Exception as e: abort(404) if str(garden) == '[]': abort(404) garden = garden[0] # same as above if plant_id not in [str(i) for i in garden.plants]: abort(401) try: plant = user_plants.objects(id=plant_id) except Exception as e: abort(404) if str(plant) == '[]': abort(404) plant = plant[0] plant_type = plant_types.objects(id=str(plant.plant_type_id)) if str(plant_type) == '[]': abort(401) plant_type = plant_type[0] #goes in place of "Water it": plant_type["watering_description"] garden.plants.remove(plant.id) garden.save() plant.delete() else: abort(401) return "", 200
def create_user(): if 'email' in session: # TODO: change based on function name return redirect(url_for('get_user_gardens')) if request.method == 'POST': with util.MongoConnect(): for user in users.objects(): if request.json['email'] == user.email: # TODO: Change later return "", 200 # insert into database, then login with util.MongoConnect(): users(email=request.json['email'], gardens=[], unsubscribed=False).save() session['email'] = request.json['email'] # TODO: change based on function name return "", 200 # TODO: change this return return {}
def get_user_gardens(): ''' Route which returns a list of all gardens in json format ''' if 'email' not in session: abort(403) user_gardens = [] with util.MongoConnect(): user = users.objects(email=session['email']) if str(user) == '[]': abort(401) user = user[0] for garden_id in user.gardens: garden = gardens.objects(id=garden_id) if garden != []: garden = garden[0] user_gardens.append(json.loads(garden.to_json())) return jsonify(user_gardens), 200
def get_user_plants_with_id(plant_id): ''' Route which returns a the specified plant in json format ''' if 'email' not in session: abort(403) if not is_valid_id(plant_id): abort(401) plants_list = {} with util.MongoConnect(): user = users.objects(email=session['email']) if str(user) == '[]': abort(401) user = user[0] # add all plants in each of the user's gardens for garden_id in user.gardens: garden = gardens.objects(id=garden_id) if garden != []: garden = garden[0] for user_plant_id in garden.plants: if plant_id != str(user_plant_id): continue try: plant = user_plants.objects(id=plant_id) except Exception as e: abort(404) if str(plant) == '[]': abort(404) plant = plant[0] plants_list = plant.to_dict() return jsonify(plants_list), 200
def get_garden(garden_id: str): ''' Route which returns a json object with a single garden ''' if 'email' not in session: abort(403) if not is_valid_id(garden_id): abort(401) if request.method == 'DELETE': with util.MongoConnect(): user = users.objects(email=session['email']) if str(user) == '[]': abort(401) user = user[0] # need to do str(i) because user.gardens is a list of ObjectIdFields if garden_id in [str(i) for i in user.gardens]: try: garden = gardens.objects(id=garden_id) except Exception as e: abort(404) if str(garden) == '[]': abort(404) garden = garden[0] user.gardens.remove(garden.id) # delete all plants in the garden for plant_id in garden.plants: plant = user_plants.objects(id=plant_id) if str(plant) == '[]': continue plant = plant[0] plant_type = plant_types.objects(id=plant.plant_type_id) if str(plant_type) == '[]': plant.delete() continue plant_type = plant_type[0] plant.delete() garden.delete() user.save() else: abort(401) elif request.method == 'PUT': expected_fields = [ 'name', 'address', 'latitudetl', 'longitudetl', 'latitudebr', 'longitudebr' ] for field in expected_fields: if field not in request.json: abort(401) with util.MongoConnect(): user = users.objects(email=session['email']) if str(user) == '[]': abort(401) user = user[0] # need to do str(i) because user.gardens is a list of ObjectIdFields if garden_id in [str(i) for i in user.gardens]: garden = gardens.objects(id=garden_id) if str(garden) == '[]': abort(404) garden = garden[0] garden.name = request.json['name'] garden.address = request.json['address'] garden.topleft_lat = request.json['latitudetl'] garden.topleft_long = request.json['longitudetl'] garden.bottomright_lat = request.json['latitudebr'] garden.bottomright_long = request.json['longitudebr'] garden.save() else: abort(401) else: garden = {} with util.MongoConnect(): user = users.objects(email=session['email']) if str(user) == '[]': abort(401) user = user[0] if garden_id in [str(i) for i in user.gardens]: garden = gardens.objects(id=garden_id) if str(garden) == '[]': abort(404) garden = json.loads(garden[0].to_json()) return jsonify(garden) return "", 200
def edit_plant_in_garden(garden_id: str, plant_id: str): expected_fields = [ 'plant_type_id', 'latitude', 'longitude', 'light_duration', 'light_intensity', 'last_watered', 'name', 'price', 'outdoors' ] if 'email' not in session: abort(403) if not is_valid_id(garden_id): abort(401) if not is_valid_id(plant_id): abort(401) # check that the right info was provided, else 401 for field in expected_fields: if field not in request.json: # Don't reject if still using the old set of expected fields if field in ['light_duration', 'light_intensity', 'price']: request.json[field] = 0 elif field == 'name': request.json['name'] = '' elif field == 'outdoors': request.json['outdoors'] = False else: abort(401) if not is_valid_id(request.json['plant_type_id']): abort(401) if request.json['last_watered'].find('.') == -1: request.json['last_watered'] = request.json['last_watered'] + '.000000' with util.MongoConnect(): user = users.objects(email=session['email']) if str(user) == '[]': abort(401) user = user[0] # need to do str(i) because user.gardens is a list of ObjectIdFields if garden_id in [str(i) for i in user.gardens]: try: garden = gardens.objects(id=garden_id) except Exception as e: abort(404) if str(garden) == '[]': abort(404) garden = garden[0] # same as above if plant_id not in [str(i) for i in garden.plants]: abort(401) try: plant_type = plant_types.objects( id=request.json['plant_type_id']) except Exception as e: abort(404) if str(plant_type) == '[]': abort(404) plant_type = plant_type[0] try: plant = user_plants.objects(id=plant_id) except Exception as e: abort(404) if str(plant) == '[]': abort(404) plant = plant[0] plant.plant_type_id = plant_type.id plant.latitude = request.json['latitude'] plant.longitude = request.json['longitude'] plant.light_duration = request.json['light_duration'] plant.light_intensity = request.json['light_intensity'] plant.name = request.json['name'] plant.price = request.json['price'] plant.outdoors = request.json['outdoors'] plant.last_watered = datetime.datetime.strptime( request.json['last_watered'], '%Y-%m-%d %H:%M:%S.%f') plant.save() else: abort(401) return "", 200