def get_status(request, status): try: posts = Post.objects.filter(status=status.upper()) print(posts[0].delete_at, posts[0]) payload = [{ "id": p.id, "title": p.title, "description": p.descriptions, "status": p.status, "category": [c.name for c in p.category.all()], "location": p.location.name, "user": getUser(p.user_id), "create_at": p.create_at, "date": p.date, "images": getImage(p.id) } for p in posts if p.delete_at is None] return JsonResponse({ "statusCode": 200, "statusText": "Success", "message": "Query Success", "error": False, "data": payload }) except: return JsonResponse({ "statusCode": 500, "statusText": "Internal Server", "message": "Internal Server", "error": True })
def suggest(request, id): if request.method == 'GET': msg_query = Message.objects.filter(send_by_id=None) suggest_id = [s.post_id for s in msg_query] user = User.objects.get(pk=id) my_posts = Post.objects.filter(user_id=id) posts = Post.objects.filter( create_at__range=[now() - timedelta( days=7), now()]) suggest = [] my_location = [p.location.name for p in my_posts] my_category = [p.category.all() for p in my_posts] my_category_flatten = [c[0].name for c in my_category] my_category_flatten = set(my_category_flatten) for post in posts: curr_post_category = [c.name for c in post.category.all()] if (post.user_id != id and post.status != "RETURNED" and post.delete_at is None and (post.location.name in my_location or set(curr_post_category).intersection( set(my_category_flatten)))): suggest.append(post) message = [ Message(text=f"We suggest this post to you: {p.title}", post=p, send_by=None, message_to=user) for p in suggest if p.id not in suggest_id ] for m in message: m.save() payload = [{ "text": m.text, "from": getUser(m.send_by_id), "create_at": m.create_at, "post": m.post_id, "messageId": m.id } for m in message if m.delete_at is None] return JsonResponse({ "statusCode": 200, "statusText": "Success", "message": "Query Success!", "error": False, "data": payload })
def post_edit(request, postId): post = Post.objects.get(pk=postId) payload = { "id": postId, "title": post.title, "description": post.descriptions, "status": post.status, "category": [c.name for c in post.category.all()], "location": post.location.name, "user": post.user.username, "create_at": post.create_at, "date": post.date, "comments": getComment(postId), "images": getImage(postId), "user": getUser(post.user_id), } return render(request, 'posts/editPost.html', context={'Posts': payload})
def get_message(request, id): try: if request.method == 'GET': messages = Message.objects.filter(message_to_id=id) payload = [{ "text": m.text, "from": getUser(m.send_by_id), "create_at": m.create_at, "post": m.post_id, "messageId": m.id } for m in messages if m.delete_at is None] return JsonResponse({ "statusCode": 200, "statusText": "Success", "message": "Query Success!", "error": False, "data": payload }) elif request.method == 'DELETE': message = Message.objects.get(pk=id) message.delete_at = now() message.save() return JsonResponse({ "statusCode": 200, "statusText": "Success", "message": "Message Deleted!", "error": False }) except: return JsonResponse({ "statusCode": 500, "statusText": "Internal Server", "message": "Internal Server", "error": True })
def userPost(request, userId): """ Get all post of specific user """ try: posts = Post.objects.filter(user_id=userId).order_by('-create_at') user = User.objects.get(pk=userId) payload = [{ "id": p.id, "title": p.title, "description": p.descriptions, "status": p.status, "category": [c.name for c in p.category.all()], "location": p.location.name, "create_at": p.create_at, "date": p.date, "images": getImage(p.id), "user": getUser(p.user.id), "comments": getComment(p.id) } for p in posts if p.delete_at is None] # return JsonResponse({ # "statusCode": 200, # "statusText": "Success", # "message": "Successt", # "error": False, # "data": payload # }) return render(request, 'account/profile.html', context={ 'Posts': payload, 'User': user }) except ObjectDoesNotExist: return JsonResponse({ "statusCode": 404, "statusText": "Not Found", "message": "Post Not Exist", "error": True })
def post_get(request, postId): """ Get Specific Post """ if request.method == 'GET': try: post = Post.objects.get(pk=postId) payload = { "id": postId, "title": post.title, "description": post.descriptions, "status": post.status, "category": [c.name for c in post.category.all()], "location": post.location.name, "user": post.user.username, "create_at": post.create_at, "date": post.date, "comments": getComment(postId), "images": getImage(postId), "user": getUser(post.user_id), } return render(request, 'posts/post.html', context={'Posts': payload}) except ObjectDoesNotExist: return JsonResponse({ "statusCode": 404, "statusText": "Not Found", "message": "Post Not Exist", "error": True }) elif request.method == 'DELETE': try: post = Post.objects.get(pk=postId) post.delete_at = now() post.save() return JsonResponse({ "statusCode": 200, "statusText": "Success", "message": "Post Deleted!", "error": False }) except ObjectDoesNotExist: return JsonResponse({ "statusCode": 404, "statusText": "Not Found", "message": "Post Not Exist", "error": True }) elif request.method == 'PUT': try: post = Post.objects.get(pk=postId) post.title = request.data[ 'title'] if 'title' in request.data else post.title post.descriptions = request.data[ 'descriptions'] if 'descriptions' in request.data else post.descriptions post.status = request.data[ 'status'] if 'status' in request.data else post.status post.save() return JsonResponse({ "statusCode": 200, "statusText": "Success", "message": "Post Edited!", "error": False }) except ObjectDoesNotExist: return JsonResponse({ "statusCode": 404, "statusText": "Not Found", "message": "Post Not Exist", "error": True })
def post(request): """ Get post for index page and create new post """ try: if request.method == 'GET': if request.GET.get('filter-search-by') == "Category": posts = Post.objects.filter(category=request.GET.get( 'filter-category')).order_by('-create_at') elif request.GET.get('filter-search-by') == "Location": posts = Post.objects.filter(location=request.GET.get( 'filter-location')).order_by('-create_at') elif request.GET.get('filter-search-by') == "Status": posts = Post.objects.filter(status=request.GET.get( 'filter-status').upper()).order_by('-create_at') else: posts = Post.objects.all().order_by('-create_at') payload = [{ "id": p.id, "title": p.title, "description": p.descriptions, "status": p.status, "location": p.location.name, "category": [c.name for c in p.category.all()], "create_at": p.create_at, "date": p.date, "user": getUser(p.user.id), "images": getImage(p.id), "comments": getComment(p.id) } for p in posts] categoryAll = Category.objects.all() locationAll = Location.objects.all() return render(request, 'posts/index.html', context={ 'Category': categoryAll, 'Locations': locationAll, 'Posts': payload }) elif request.method == 'POST': try: # Create Post post = Post(title=request.data['title'], descriptions=request.data['descriptions'], status=request.data['status'], date=request.data['date'], user=User.objects.get(pk=request.data['user_id']), location=Location.objects.get( pk=request.data['location_id'])) # Add category to post post.save() for c in request.data['categories_id']: post.category.add(Category.objects.get(pk=c)) post.save() # Create PostImage for i in request.data['images']: image = PostImage(image_url=i, post=post) image.save() return JsonResponse({ "statusCode": 201, "statusText": "Created", "message": "Create Post Successfully", "error": False, }) except KeyError: return JsonResponse({ "statusCode": 400, "statusText": "Bad Request", "message": "Invalid API Payload", "error": True }) except: return JsonResponse({ "statusCode": 500, "statusText": "Internal Server", "message": "Internal Server", "error": True })