Example #1
0
    def put(self, **kwargs):
        """Put method handler for notification route."""
        user_id = kwargs.get('user_id')
        notification_id = kwargs.get('notification_id')

        if NotificationModel.exist_users(user_id):
            if NotificationModel.exists(notification_id):
                notification = NotificationModel.get_single(notification_id)
                if notification.user_id == user_id:
                    NotificationModel.mark_read(notification_id)
                    return Response(status=201, mimetype='application/json')
                else:
                    res = {'error': ['invalid user id']}
                    return Response(json.dumps(ErrorResponse().dump(res).data),
                                    status=400,
                                    mimetype='application/json')
            else:
                res = {
                    'error': [
                        'notification {id} does not exists'.format(
                            id=notification_id)
                    ]
                }
                return Response(json.dumps(ErrorResponse().dump(res).data),
                                status=400,
                                mimetype='application/json')
        else:
            res = {'error': ['user {id} does not exists'.format(id=user_id)]}
            return Response(json.dumps(ErrorResponse().dump(res).data),
                            status=400,
                            mimetype='application/json')
    def get(self, **kwargs):
        """GET method handler for downloading files with paths."""
        path = unquote(kwargs.get('path'))

        if path is None or not path:
            err = {'error': [_('path cannot be empty')]}
            return Response(app.json_encoder.encode(
                ErrorResponse().dump(err).data),
                            status=422,
                            mimetype='application/json')
        if os.path.isfile(path):
            try:
                return send_file(path, as_attachment=True)
            except Exception as e:
                app.logger.exception(e)
                res = {'error': [_('Unable to process the request')]}
                return Response(app.json_encoder.encode(
                    ErrorResponse().dump(res).data),
                                status=500,
                                mimetype='application/json')
        else:
            res = {'error': [_('file not found or bad file path')]}
            return Response(app.json_encoder.encode(
                ErrorResponse().dump(res).data),
                            status=400,
                            mimetype='application/json')
 def get(self, **kwargs):
     """Get method handler for notification route."""
     user_id = kwargs.get('user_id')
     if user_id:
         notifications = NotificationModel.get(user_id)
         response = Notifications().dump(dict(notifications=notifications)).data
         return Response(json.dumps(response), status=200, mimetype='application/json')
     else:
         err = {'error': ['user_id cannot be empty']}
         return Response(json.dumps(ErrorResponse().dump(err).data),
                         status=422, mimetype='application/json')