Exemple #1
0
async def comment_car(comment_payload: CommentType, car_id: str, request: Request, response: Response):
  # {
	#   "comment":"ramdenad qiravdeba ?"
  # }
  
  # Get logged in user
  user = auth_module.get_me(request=request)

  if user is None:
    response.status_code = status.HTTP_401_UNAUTHORIZED
    return util_module.generate_response_context(status=401, error='access denied!', data=None)

  # Query car fields
  car = Car.objects.get(_id=car_id)

  # Create new comment
  comment = Comment()
  comment.comment = comment_payload.comment
  comment.car = car_id

  # # Create author
  author = CommentAuthor()
  author.surname = user["name"]
  author.name = user["surname"]
  author.user = user["_id"]

  # Append author
  comment.author = author
  comment.save()

  # Register new activity
  activity = Activty()
  activity.action = "CommentedCar"
  activity.user = user["_id"]

  # Activity subject
  subject = ActivitySubject()
  subject.car = car_id
  subject.title = car.title
  subject.model = car.model
  activity.car = subject
  activity.save()

  # Generate JSON
  json_data = comment.to_json()
  data = json.loads(json_data)

  # Return comment back
  response.status_code = status.HTTP_200_OK
  return util_module.generate_response_context(status=201, error=None, data=data)
Exemple #2
0
def update_video(comment_payload: CommentType, video_id: str, request: Request,
                 response: Response, action: str):

    user = auth_module.get_me(request=request)

    if user is None:
        response.status_code = status.HTTP_401_UNAUTHORIZED
        return util_module.generate_response_context(status=403,
                                                     error='access denied!',
                                                     data=None)

    # Update video
    if action == 'update':
        return {"msg": "update video!"}

    # Trash video
    if action == 'trash':

        # Perform clean delete
        Video.objects(pk=video_id).update(set__status="deleted")

        return {"msg": "trashed"}

    if action == 'upvote':

        # Push user to upvotes arrat
        Video.objects(pk=video_id).update(
            add_to_set__upvotes=[user.get("_id")],
            pull__downvotes=user.get("_id"))

        return {"msg": "upvoting video"}

    if action == 'downvote':

        # Push user to downvotes array
        Video.objects(pk=video_id).update(
            add_to_set__downvotes=[user.get("_id")],
            pull__upvotes=user.get("_id"),
        )

        return {"msg": "downvote video"}

    if action == 'comment':

        # Initialize comment
        comment = Comment()
        comment.video = video_id
        comment.comment = comment_payload.comment

        # Initialize author
        author = CommentAuthor()
        author.name = user.get("name")
        author.surname = user.get("surname")
        author.user = user.get("_id")

        # Append author and save
        comment.author = author
        comment.save()

        return {"msg": "comment added!"}

    # Provide action
    return {"msg": "updating video!"}