Beispiel #1
0
    def put(self):
        try:
            user = current_identity.user()
            if user is None:
                return {
                    "message": "Unable to find user information"
                }, status.HTTP_401_UNAUTHORIZED

            parser = reqparse.RequestParser()
            parser.add_argument("event_id", type=str, location="json")
            parser.add_argument("text_context", type=str, location="json")
            body = parser.parse_args()

            event = BaseEvent.objects(id=body.event_id).first()
            if event is None or event not in user.posted_events:
                return {
                    "message": "no permission to delete"
                }, status.HTTP_203_NON_AUTHORITATIVE_INFORMATION

            event.update(text_context=body.text_context,
                         is_edited=True,
                         last_edit_at=datetime.utcnow())
            user.update(last_online=datetime.utcnow())
            return event.get_complete_json(), status.HTTP_200_OK
        except Exception as e:
            return {"error": str(e)}, status.HTTP_400_BAD_REQUEST
Beispiel #2
0
    def post(self):
        try:
            user = current_identity.user()
            if user is None:
                return {
                    "message": "Unable to find user information"
                }, status.HTTP_401_UNAUTHORIZED

            parser = reqparse.RequestParser()
            parser.add_argument("event_id", type=str, location="json")
            body = parser.parse_args()

            event = BaseEvent.objects(id=body.event_id).first()
            if event is None:
                return {
                    "message": "event does not exists"
                }, status.HTTP_501_NOT_IMPLEMENTED

            if event in user.posted_events:
                return {
                    "message": "can not report your own event"
                }, status.HTTP_203_NON_AUTHORITATIVE_INFORMATION

            # Enough permission and information -> remove event
            user.update(add_to_set__reported_events=event,
                        last_online=datetime.utcnow())
            event.update(add_to_set__reported_by=user)
            return {"message": "success"}, status.HTTP_200_OK
        except Exception as e:
            return {"error": str(e)}, status.HTTP_400_BAD_REQUEST
Beispiel #3
0
    def post(self):
        try:
            user = current_identity.user()
            if user is None:
                return {
                    "message": "Unable to find user information"
                }, status.HTTP_401_UNAUTHORIZED

            parser = reqparse.RequestParser()
            parser.add_argument("text_context", type=str, location="json")
            parser.add_argument("images", type=list, location="json")
            parser.add_argument("contain_event", type=str, location="json")
            parser.add_argument("privacy", type=str, location="json")
            parser.add_argument("longitude", type=float, location="json")
            parser.add_argument("latitude", type=float, location="json")
            parser.add_argument("tags", type=list, location="json")
            body = parser.parse_args()

            event = BaseEvent(create_by=user,
                              text_context=body.text_context,
                              images=body.images,
                              contain_event=body.contain_event,
                              privacy=body.privacy,
                              location=[body.longitude, body.latitude],
                              tags=body.tags).save()

            # Now create an action
            action = BaseAction(
                by_user=user,
                action_type=app_constant.action_create_new_event,
                target=event.id,
                action_priority=app_constant.priority_medium).save()
            user.update(add_to_set__posted_events=event,
                        add_toset__recent_actions=action,
                        last_online=datetime.utcnow())

            return event.get_initial_json(), status.HTTP_200_OK
        except Exception as e:
            return {"error": str(e)}, status.HTTP_400_BAD_REQUEST
Beispiel #4
0
    def post(self):
        try:
            user = current_identity.user()
            if user is None:
                return {
                    "message": "Unable to find user information"
                }, status.HTTP_401_UNAUTHORIZED

            parser = reqparse.RequestParser()
            parser.add_argument("event_id", type=str, location="json")
            parser.add_argument("text_content", type=str, location="json")
            parser.add_argument("image", type=str, location="json")
            body = parser.parse_args()

            event_id = body.event_id
            text_content = body.text_content
            image = body.image

            # Now file the event and then add comment to it
            event = BaseEvent.objects(id=event_id).first()
            if event is None:
                return {
                    "message": "event not found"
                }, status.HTTP_204_NO_CONTENT

            comment = BaseComment(create_by=user,
                                  text_content=text_content,
                                  image=image).save()
            event.update(add_to_set__comments=comment)

            # Now create an action
            action = BaseAction(
                by_user=user,
                action_type=app_constant.action_comment_event,
                target=event.id,
                content=comment.id,
                action_priority=app_constant.priority_low).save()
            user.update(last_online=datetime.utcnow(),
                        add_to_set__recent_actions=action)

            return {"message": "request success"}, status.HTTP_200_OK
        except ValueError as err:
            return {"error": str(err)}, status.HTTP_400_BAD_REQUEST
        except Exception as e:
            return {"error": str(e)}, status.HTTP_400_BAD_REQUEST
Beispiel #5
0
    def delete(self):
        try:
            user = current_identity.user()
            if user is None:
                return {"message": "Unable to find user information"}, status.HTTP_401_UNAUTHORIZED

            # Delete everything created by the current user
            # Todo: Need to delete references of things created by this user
            for action in BaseAction.objects(by_user=user):
                action.delete()

            for comment in BaseComment.objects(create_by=user):
                comment.delete()

            for event in BaseEvent.objects(create_by=user):
                event.delete()

            user.delete()
            return {"message": "delete account complete"}, status.HTTP_200_OK
        except Exception as e:
            return {"message": str(e)}
Beispiel #6
0
    def delete(self):
        try:
            user = current_identity.user()
            if user is None:
                return {"message": "Unable to find user information"}, status.HTTP_401_UNAUTHORIZED

            parser = reqparse.RequestParser()
            parser.add_argument("event_id", type=str, location="json")

            body = parser.parse_args()
            event = BaseEvent.objects(id=body.event_id).first()
            if event is None:
                return {"message": "failed to find event"}, status.HTTP_204_NO_CONTENT

            if user not in event.liked_by:
                return {"message": "you did not like the event"}, status.HTTP_204_NO_CONTENT

            event.update(pull__liked_by=user)
            user.update(last_online=datetime.utcnow())
        except Exception as e:
            return {"error": str(e)}, status.HTTP_400_BAD_REQUEST
Beispiel #7
0
    def post(self):
        try:
            user = current_identity.user()
            if user is None:
                return {"message": "Unable to find user information"}, status.HTTP_401_UNAUTHORIZED

            parser = reqparse.RequestParser()
            parser.add_argument("event_id", type=str, location="json")

            body = parser.parse_args()
            event = BaseEvent.objects(id=body.event_id).first()
            if event is None:
                return {"message": "failed to find event"}, status.HTTP_204_NO_CONTENT

            event.update(add_to_set__liked_by=user)

            # Now create an action
            action = BaseAction(by_user=user, action_type=app_constant.action_like_event, target=event.id,
                                action_priority=app_constant.priority_low).save()
            user.update(add_to_set__recent_actions=action, last_online=datetime.utcnow())
        except Exception as e:
            return {"error": str(e)}, status.HTTP_400_BAD_REQUEST
Beispiel #8
0
    def get(self, _from, _to):
        try:
            user = current_identity.user()
            if user is None:
                return {
                    "message": "Unable to find user information"
                }, status.HTTP_401_UNAUTHORIZED

            parser = reqparse.RequestParser()
            parser.add_argument("event_id", type=str, location="json")
            body = parser.parse_args()

            event_id = body.event_id

            event = BaseEvent.objects(id=event_id).first()
            if event is None:
                return {
                    "message": "event not found"
                }, status.HTTP_204_NO_CONTENT

            comments = event.comments
            comments.sort(operator.attrgetter('liked_by'))

            if _from > len(comments):
                return [], status.HTTP_200_OK
            elif _to >= len(comments):
                return [
                    comment.get_complete_json() for comment in comments[_from:]
                ], status.HTTP_200_OK
            else:
                return [
                    comment.get_complete_json()
                    for comment in comments[_from:_to]
                ], status.HTTP_200_OK
        except ValueError as err:
            return {"error": str(err)}, status.HTTP_400_BAD_REQUEST
        except Exception as e:
            return {"error": str(e)}, status.HTTP_400_BAD_REQUEST
Beispiel #9
0
    def delete(self):
        try:
            user = current_identity.user()
            if user is None:
                return {
                    "message": "Unable to find user information"
                }, status.HTTP_401_UNAUTHORIZED

            parser = reqparse.RequestParser()
            parser.add_argument("event_id", type=str, location="json")
            parser.add_argument("comment_id", type=str, location="json")
            body = parser.parse_args()

            event_id = body.event_id
            comment_id = body.comment_id

            # Now file the event and then add comment to it
            event = BaseEvent.objects(id=event_id).first()
            if event is None:
                return {
                    "message": "event not found"
                }, status.HTTP_204_NO_CONTENT

            comment = BaseComment.objects(id=comment_id).first()
            if comment is None:
                return {
                    "message": "unable to find event"
                }, status.HTTP_204_NO_CONTENT

            event.update(pull__comments=comment)
            user.update(last_online=datetime.utcnow())
            comment.delete()
            return {"message": "request success"}, status.HTTP_200_OK
        except ValueError as err:
            return {"error": str(err)}, status.HTTP_400_BAD_REQUEST
        except Exception as e:
            return {"error": str(e)}, status.HTTP_400_BAD_REQUEST