Files
eamco_office_api/app/social/views.py

91 lines
2.8 KiB
Python

import logging
from flask import request
import datetime
from app.social import social
from app import db
from app.common.responses import success_response
from app.classes.customer_social import (Customer_Customer_Social_schema,
Customer_Customer_Social)
from flask_login import login_required
logger = logging.getLogger(__name__)
@social.route("/posts/<int:customer_id>/<int:page>", methods=["GET"])
@login_required
def get_customer_posts(customer_id, page):
logger.info(f"GET /social/posts/{customer_id}/{page} - Fetching customer posts page {page}")
per_page_amount = 50
if page is None:
offset_limit = 0
elif page == 1:
offset_limit = 0
else:
offset_limit = (per_page_amount * page) - per_page_amount
customer_posts = (db.session
.query(Customer_Customer_Social)
.filter(Customer_Customer_Social.customer_id == customer_id)
.order_by(Customer_Customer_Social.id.desc())
.limit(per_page_amount).offset(offset_limit))
customer_social_schema = Customer_Customer_Social_schema(many=True)
return success_response({"posts": customer_social_schema.dump(customer_posts)})
@social.route("/create/<int:customer_id>", methods=["POST"])
@login_required
def create_post(customer_id):
logger.info(f"POST /social/create/{customer_id} - Creating new social post")
comment = request.json["comment"]
poster_employee_id = request.json["poster_employee_id"]
create_post = Customer_Customer_Social(
created = datetime.datetime.utcnow(),
customer_id = customer_id,
poster_employee_id = poster_employee_id,
comment = comment
)
db.session.add(create_post)
db.session.commit()
return success_response()
@social.route("/posts/<int:post_id>", methods=["PATCH"])
@login_required
def edit_post(post_id):
logger.info(f"PATCH /social/posts/{post_id} - Editing social post")
customer_post = (db.session
.query(Customer_Customer_Social)
.filter(Customer_Customer_Social.id == post_id)
.first())
comment = request.json["comment"]
customer_post.comment = comment
db.session.add(customer_post)
db.session.commit()
return success_response()
@social.route("/delete/<int:post_id>", methods=["DELETE"])
@login_required
def delete_post(post_id):
logger.info(f"DELETE /social/delete/{post_id} - Deleting social post")
customer_post = (db.session
.query(Customer_Customer_Social)
.filter(Customer_Customer_Social.id == post_id)
.first())
db.session.delete(customer_post)
db.session.commit()
return success_response()