def edit_comment(id): user_id = current_user.get_id() # user_id = 1 form = EditCommentForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): comment = Comment.query.get(id) comment.content = form.data['content'], comment.last_modified = datetime.datetime.now() db.session.commit() return comment.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)}
def update_pet(petId): """ Update pet """ form = CreatePetForm() form["csrf_token"].data = request.cookies["csrf_token"] image_error = [] image = request.files.get("image", None) pet_to_update = Pet.query.get(petId) print("pet to update ------------------ ", pet_to_update.to_dict()) pet_to_update.name = form.data["name"] pet_to_update.petType = form.data["petType"] pet_to_update.age = form.data["age"] pet_to_update.energy = form.data["energy"] pet_to_update.social = form.data["social"] pet_to_update.behaved = form.data["behaved"] pet_to_update.size = form.data["size"] pet_to_update.env = form.data["env"] pet_to_update.description = form.data["description"] if image is not None: image.filename = secure_filename(image.filename) pattern = re.compile(".*(apng|avif|jpe?g|png|svg|webp)$", re.IGNORECASE) is_image = bool(pattern.match(image.mimetype)) if not is_image: image_error.append( "Upload must be an image (apng, avif, jpeg/jpg, png, svg, webp)." ) if form.validate_on_submit() and not image_error: output_link = upload_file_to_s3(image) if image else None if output_link: pet_to_update.imageURL = output_link print("updated pet ------------------ ", pet_to_update.to_dict()) db.session.add(pet_to_update) db.session.commit() return pet_to_update.to_dict() errors = validation_errors_to_error_messages(form.errors) errors += image_error return {"errors": errors}
def edit_expense(transaction_id): form = ExpenseForm() form['csrf_token'].data = request.cookies['csrf_token'] # print(request.get_json(), "<===== JSON object here") if form.validate_on_submit(): # Edit the transaction. transaction_to_edit = Transaction.query.get(transaction_id) transaction_to_edit.group_id = form.data["group_id"] transaction_to_edit.payer_id = form.data["payer_id"] transaction_to_edit.paid_amount = form.data["amount"] transaction_to_edit.description = form.data["description"] transaction_to_edit.expense_date = form.data["expense_date"] transaction_to_edit.completed = False transaction_to_edit.updated_at = date.today() db.session.commit() # For each debtor, update or delete their expense. payed_amount = form.data["amount"] split_by = len(form.data["debtors"]) + 1 remainder = payed_amount % split_by debtor_pays = (payed_amount - remainder) / split_by new_debtors = form.data["debtors"] for previous_expense in transaction_to_edit.expenses: previous_debtor = previous_expense.borrower_id if previous_debtor in new_debtors: previous_expense.lender_id = form.data["payer_id"] previous_expense.amount = debtor_pays previous_expense.completed = form.data["completed"] previous_expense.updated_at = date.today() new_debtors.remove(previous_debtor) else: db.session.delete(previous_expense) for new_debtor in new_debtors: new_expense = TransactionExpense( transaction_id=transaction_to_edit.id, lender_id=form.data["payer_id"], borrower_id=new_debtor, amount=debtor_pays, completed=form.data["completed"], updated_at=date.today()) db.session.add(new_expense) db.session.commit() return {'message': 'Transaction Updated!'} return {'errors': validation_errors_to_error_messages(form.errors)}, 400
def update_project(projectId): ''' Updates a project's details ''' project_to_update = Project.query.get(projectId) form = ProjectForm() # Do we need line below? form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): title = form.data['title'] project_to_update.title = title db.session.commit() return {'message': 'Project Updated!'} return {'errors': validation_errors_to_error_messages(form.errors)}, 400
def edit_post(id): user_id = current_user.get_id() # user_id = 1 user = User.query.get(user_id) form = EditPostForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): post = Post.query.get(id) post.title = form.data['title'], post.content = form.data['content'], post.last_modified = datetime.datetime.now() db.session.commit() return post.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)}
def create_pet(): """ Create new pet """ form = CreatePetForm() form["csrf_token"].data = request.cookies["csrf_token"] image_error = [] image = request.files.get("image", None) if image != None: image.filename = secure_filename(image.filename) pattern = re.compile(".*(apng|avif|jpe?g|png|svg|webp)$", re.IGNORECASE) is_image = bool(pattern.match(image.mimetype)) if not is_image: image_error.append( "Upload must be an image (apng, avif, jpeg/jpg, png, svg, webp)." ) if form.validate_on_submit() and not image_error: output_link = ( upload_file_to_s3(image) if image else "https://pair-yo-pet-aws.s3-us-west-1.amazonaws.com/default-dog.png" ) new_pet = Pet( userId=form.data["userId"], name=form.data["name"], petType=form.data["petType"], age=form.data["age"], imageURL=output_link, energy=form.data["energy"], social=form.data["social"], behaved=form.data["behaved"], size=form.data["size"], env=form.data["env"], description=form.data["description"], ) db.session.add(new_pet) db.session.commit() return new_pet.to_dict() errors = validation_errors_to_error_messages(form.errors) errors += image_error return {"errors": errors}
def update_display_name(display_name_id): """ Update display name """ form = DisplayNameForm() form["csrf_token"].data = request.cookies["csrf_token"] updated_name = DisplayName.query.get(display_name_id) updated_name.display_name = form.data["display_name"] db.session.add(updated_name) db.session.commit() return updated_name.to_dict() errors = validation_errors_to_error_messages(form.errors) return {"errors": errors}
def create_display_name(): """ Create display name """ form = DisplayNameForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): new_display_name = DisplayName( nameId=form.data["nameId"], display_name=form.data["display_name"], ) db.session.add(new_display_name) db.session.commit() return new_display_name.to_dict() errors = validation_errors_to_error_messages(form.errors) return {"errors": errors}
def make_post(): user_id = current_user.get_id() # user_id = 1 user = User.query.get(user_id) form = NewPostForm() form['user_id'].data = user_id form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): post = Post(user_id=form.data['user_id'], title=form.data['title'], content=form.data['content'], created_on=datetime.datetime.now(), last_modified=datetime.datetime.now()) db.session.add(post) db.session.commit() return post.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)}
def update_joke(jokeId): """ Update joke """ form = CreateJokeForm() form["csrf_token"].data = request.cookies["csrf_token"] image_error = [] image = request.files.get("image", None) joke_to_update = Joke.query.get(jokeId) print("joke to update ------------------ ", joke_to_update.to_dict()) joke_to_update.joke = form.data["joke"] joke_to_update.jokeType = form.data["jokeType"] if image is not None: image.filename = secure_filename(image.filename) pattern = re.compile(".*(apng|avif|jpe?g|png|svg|webp)$", re.IGNORECASE) is_image = bool(pattern.match(image.mimetype)) if not is_image: image_error.append( "Upload must be an image (apng, avif, jpeg/jpg, png, svg, webp)." ) if form.validate_on_submit() and not image_error: output_link = upload_file_to_s3(image) if image else None if output_link: joke_to_update.imageURL = output_link print("updated joke ------------------ ", joke_to_update.to_dict()) db.session.add(joke_to_update) db.session.commit() return joke_to_update.to_dict() errors = validation_errors_to_error_messages(form.errors) errors += image_error return {"errors": errors}
def update_thread(threadId): """ Update thread """ form = CreateThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] thread_to_update = Thread.query.get(threadId) thread_to_update.comment = form.data["comment"] if form.validate_on_submit(): db.session.add(thread_to_update) db.session.commit() return thread_to_update.to_dict() errors = validation_errors_to_error_messages(form.errors) return {"errors": errors}
def create_message(): """ Create new message """ form = CreateMessageForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): new_message = Message( senderId=form.data["senderId"], receiverId=form.data["receiverId"], message=form.data["message"], ) db.session.add(new_message) db.session.commit() return new_message.to_dict() errors = validation_errors_to_error_messages(form.errors) return {"errors": errors}
def create_tasks(projectId): ''' Create a task within a project ''' form = TaskForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): task = Task( userId=current_user.id, title=form.data['title'], description=form.data['description'], projectId=projectId, completed=form.data['completed'] ) db.session.add(task) db.session.commit() return task.to_dict() # return {'errors': validation_errors_to_error_messages} return {'errors': validation_errors_to_error_messages(form.errors)}, 500
def update_task(projectId, taskId): ''' Updates a project's details ''' task_to_update = Task.query.get(taskId) form = TaskForm() # Do we need line below? form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): print("We are sending a PUT request...", form.data) title = form.data['title'] description = form.data['description'] completed = form.data['completed'] task_to_update.title = title task_to_update.description = description task_to_update.completed = completed print("ended the PUT request", task_to_update.title) db.session.commit() return {'message': 'Task Updated!'} return {'errors': validation_errors_to_error_messages(form.errors)}, 400
def add_project(): ''' Create a project ''' form = ProjectForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): project = Project( # Current user didn't work... userId=current_user.id, # userId=1, title=form.data['title'], completed=False ) db.session.add(project) db.session.commit() return project.to_dict() # This gets put into the project array # return {"projects": [project.to_dict() for project in projects]} return {'errors': validation_errors_to_error_messages(form.errors)}, 500
def create_joke(): """ Create new joke """ form = CreateJokeForm() form["csrf_token"].data = request.cookies["csrf_token"] image_error = [] image = request.files.get("image", None) if image is not None: image.filename = secure_filename(image.filename) pattern = re.compile(".*(apng|avif|jpe?g|png|svg|webp)$", re.IGNORECASE) is_image = bool(pattern.match(image.mimetype)) if not is_image: image_error.append( "Upload must be an image (apng, avif, jpeg/jpg, png, svg, webp)." ) if form.validate_on_submit() and not image_error: output_link = ( upload_file_to_s3(image) if image else "https://pair-yo-pet-aws.s3-us-west-1.amazonaws.com/default-dog.png" ) new_joke = Joke( userId=form.data["userId"], joke=form.data["joke"], imageURL=output_link, jokeType=form.data["jokeType"], ) db.session.add(new_joke) db.session.commit() return new_joke.to_dict() errors = validation_errors_to_error_messages(form.errors) errors += image_error return {"errors": errors}
def create_joke(): """ Create new joke """ form = CreateThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): new_thread = Thread( userId=form.data["userId"], jokeId=form.data["jokeId"], comment=form.data["comment"], ) db.session.add(new_thread) db.session.commit() print("----------newthread----", new_thread) return new_thread.to_dict() errors = validation_errors_to_error_messages(form.errors) return {"errors": errors}
def create_group(): """ Creates a new group """ form = GroupForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): group = Group( name=form.data['name'], ) db.session.add(group) # get user id's to add, get user objects, and add user to group object. for user_id in form.data["users"]: user_in_group = User.query.get(user_id) group.users.append(user_in_group) db.session.commit() return {'message': 'Group Created!'} return {'errors': validation_errors_to_error_messages(form.errors)}, 400
def update_group(group_id): """ Updates a group """ group_to_update = Group.query.get(group_id) form = GroupForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): name = form.data['name'] group_to_update.name = name # reset group's users array. group_users = [] for user_id in form.data["users"]: user_in_group = User.query.get(user_id) group_users.append(user_in_group) group_to_update.users = group_users db.session.commit() return {'message': 'Group Updated!'} return {'errors': validation_errors_to_error_messages(form.errors)}, 400
def create_expense(): form = ExpenseForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): # Create the new transaction. new_transaction = Transaction(group_id=form.data["group_id"], payer_id=form.data["payer_id"], paid_amount=form.data["amount"], description=form.data["description"], expense_date=form.data["expense_date"], completed=False, updated_at=date.today()) db.session.add(new_transaction) db.session.commit() requestinfo = request.json # For each debtor, create a new transaction expense. print("LOOK HERE ========", requestinfo) payed_amount = form.data["amount"] split_by = len(form.data["debtors"]) + 1 remainder = payed_amount % split_by debtor_pays = (payed_amount - remainder) / split_by all_associated_transaction = [] for debtor in form.data["debtors"]: info = [ new_transaction.id, form.data["payer_id"], debtor, debtor_pays, False, date.today() ] print(info, "LOOK ===== HERE -========") new_expense = TransactionExpense(transaction_id=new_transaction.id, lender_id=form.data["payer_id"], borrower_id=debtor, amount=debtor_pays, completed=False, updated_at=date.today()) users = requestinfo['groupUsers'] payer_id = requestinfo["payer_id"] debtor_firstname = "" if payer_id == current_user.id: current_user_lender = 'You' else: for user in users: if user['user_id'] == payer_id: current_user_lender = user['first_name'] for user in users: if user['user_id'] == debtor: debtor_firstname = user['first_name'] all_associated_transaction.append({ 'payer_id': payer_id, 'paid_amount': payed_amount, 'expense_date': requestinfo["expense_date"], 'borrower_id': debtor, 'first_name': debtor_firstname, 'amount': debtor_pays, 'description': requestinfo['description'], 'current_user_lender': current_user_lender }) db.session.add(new_expense) db.session.commit() response_transaction = {new_transaction.id: all_associated_transaction} return { 'message': 'Transaction Created!', 'transaction': response_transaction } return {'errors': validation_errors_to_error_messages(form.errors)}, 400