def create_list(): data = request.get_json() list_name = data['list_name'] new_list = List(list_name=list_name) if new_list.save(): flash(f'Successfully created a new list,{ list_name }!', 'success') else: flash('Did not create a new list', 'danger') result = jsonify({'list_name': list_name}) return result
def __init__(self, board_id, trello_settings): self.board_id = board_id self.trello_settings = trello_settings try: self.name = requests.get( TRELLO_BOARD_REQUEST.format( board_id=self.board_id, app_id=self.trello_settings['app_id'], token=self.trello_settings['token'], )).json()['name'] except ValueError: raise TrelloTokenError self.load_list_names() self.doing = List(self, DOING_LIST_ID)
def post(self): #data = request.get_json() post_user = List().save() output = post_user return generate_json_response(result=output)
def add_new_list(client_list, user): list = List(client_list["name"], user.id, user.name, client_list.get("image")) db.get_session().add(list) db.get_session().commit() return list
def listRequests(): list_id = request.args.get('list_id', None) user_id = request.args.get('user_id', None) auth_token = token_getter() if request.method == 'GET': # checks to see if the client is logged-in, by seeing if an auth_token exists, if not then `auth_token = -1` if type(auth_token) is not int: auth_token = -1 # 1|0| Case 1: Where only the list_id is provided if list_id: try: get_list = List.query.filter_by(id=list_id).first() list_privacy = get_list.private list_user_id = get_list.user_id if list_privacy == False or int(list_user_id) == int(auth_token): return jsonify(get_list.serialize) else: return jsonify({"error": "list is set to private"}), 403 except Exception as e: return jsonify({'error': "{}".format(e.__cause__)}), 400 # 0|1 Case 2: when only user_id is provided if user_id: try: if int(user_id) == int(auth_token): lists = List.query.filter_by(user_id=user_id) return jsonify([list.serialize for list in lists]), 200 else: lists = List.query.filter_by( user_id=user_id, private=False) return jsonify([list.serialize for list in lists]), 200 except Exception as e: return jsonify({'error': "{}".format(e.__cause__)}), 400 # 0|0 Case 3: when none is provided, we look at the auth_token and retrieve the lists if auth_token != -1: try: lists = List.query.filter_by(user_id=auth_token) return jsonify([list.serialize for list in lists]), 200 except Exception as e: return jsonify({'error': "{}".format(e.__cause__)}), 400 if request.method == 'POST': # client must be logged-in in order to create a list, client is only restricted to create list for their own account if type(auth_token) is not int: return jsonify({'error': "you must log in to create a list"}), 400 else: body = json.loads(request.get_data()) list_user_id = int(auth_token) list_name = body['name'] # checks to see if list already exists if not List.query.filter_by(user_id=list_user_id, name=list_name).first(): # checks to see if cloudinary works new_img_url = replace_cloudinary_image(body['img_url']) try: list = List(list_user_id, list_name, new_img_url) db.session.add(list) db.session.commit() except Exception as e: return jsonify({'error': "{}".format(e.__cause__)}), 400 return jsonify({'response': 'list successfully added'}), 200 else: return jsonify({'error': 'list already exists'}), 400 if request.method == 'PUT': # client must be logged-in in order to edit a list, client is only restricted to editting their own lists if type(auth_token) is not int: return jsonify({'error': "you must log in to edit the list"}), 400 else: list = List.query.get(int(list_id)) if list.user_id == int(auth_token): try: req = request.get_json() list.name = req.get("name", list.name) list.private = req.get("private", list.private) if "img_url" in req: list.img_url = replace_cloudinary_image(req["img_url"]) db.session.commit() return jsonify({"response": "List '{}' was updated".format(list_id)}), 200 except Exception as e: return jsonify({'error': "{}".format(e.__cause__)}), 400 else: return jsonify({'error': "unauthorized access"}), 401 if request.method == 'DELETE': # client must be logged-in in order to delete a list, client is only restricted to deleting their own lists if type(auth_token) is not int: return jsonify({'error': "you must logged-in, to delete the list"}), 400 else: list = List.query.get(int(list_id)) if list.user_id == int(auth_token): try: list_to_product = ListToProduct.query.filter_by( list_id=int(list_id)).delete() db.session.delete(list) db.session.commit() return jsonify({'response': "List '{}' was successfully deleted from the database".format(list)}), 200 except Exception as e: return jsonify({'error': "{}".format(e.__cause__)}), 400 else: return jsonify({'error': "unauthorized access"}), 401