Example #1
0
async def add_post(post_body: PostForm,
                   identity: str = Depends(get_jwt_identity)):
    try:
        user = get_admin_user(identity)
        try:
            postType = class_name_to_class(__name__, post_body.post)
            if not is_post(postType):
                raise InvalidPostTypeError
        except Exception:
            raise InvalidPostTypeError
        obj = postType(**post_body.obj)
        obj.author = user

        postTypeName = postType.__name__

        # Do any aditional logic here.
        # Just check with a simple `if postTypeName == POSTTYPENAME:` to see the class name coming in. Do not rely on the post variable

        obj.save()
        return obj.serialize()
    except (DoesNotExist, UnauthorizedError):
        raise UnauthorizedError('User is not admin')
    except InvalidPostTypeError:
        raise InvalidPostTypeError()
    except (FieldDoesNotExist, ValidationError, SchemaValidationError):
        raise SchemaValidationError()
    except Exception as e:
        raise e
Example #2
0
async def get_post(post: str,
                   id: str,
                   withSchema: Optional[bool] = False,
                   identity: str = Depends(get_jwt_identity)):
    try:
        get_admin_user(identity)
    except (DoesNotExist, UnauthorizedError):
        raise UnauthorizedError('User is not admin')
    try:
        try:
            postType = class_name_to_class(__name__, post)
            if not is_post(postType):
                raise InvalidPostTypeError
        except Exception:
            raise InvalidPostTypeError
        obj = postType.objects.get(id=id)
        out = {'obj': obj.serialize()}
        if withSchema:
            out['schema'] = postType.schema()
        return out
    except DoesNotExist:
        raise NotFoundError()
    except InvalidPostTypeError:
        raise InvalidPostTypeError()
    except Exception as e:
        raise e
Example #3
0
async def get_posts(post: str,
                    page: Optional[int] = None,
                    size: Optional[int] = None,
                    search: Optional[str] = None,
                    identity: str = Depends(get_jwt_identity)):
    try:
        get_admin_user(identity)
        try:
            postType = class_name_to_class(__name__, post)
            if not is_post(postType):
                raise InvalidPostTypeError
        except Exception:
            raise InvalidPostTypeError
        posts = postType.objects
        if search:
            posts.search_text(search).order_by('$text_score')
        if page == None:
            page = 0
            size = posts.count()
        elif size == None:
            raise SchemaValidationError
        return {
            'count':
            posts.count(),
            'posts':
            list(
                map(lambda p: p.serialize(),
                    posts[page * size:page * size + size]))
        }
    except (DoesNotExist, UnauthorizedError):
        raise UnauthorizedError('User is not admin')
    except InvalidPostTypeError:
        raise InvalidPostTypeError()
    except Exception as e:
        raise e
Example #4
0
async def get_posts(post: str,
                    page: Optional[int] = None,
                    size: Optional[int] = None,
                    search: Optional[str] = None):
    try:
        try:
            postType = class_name_to_class(__name__, post)
        except Exception:
            raise InvalidPostTypeError
        if not is_post(postType):
            raise InvalidPostTypeError
        posts = postType.objects()
        if search:
            posts = posts.search_text(search).order_by('$text_score')
        if page == None:
            page = 0
            size = posts.count()
        elif size == None:
            raise SchemaValidationError
        return {
            'total':
            posts.count(),
            'posts':
            list(
                map(lambda p: p.serialize(),
                    posts[page * size:page * size + size]))
        }
    except InvalidPostTypeError:
        raise InvalidPostTypeError().http_exception
    except SchemaValidationError:
        raise SchemaValidationError().http_exception
    except Exception as e:
        raise e
Example #5
0
async def is_post_slug_taken(post: str, slug: str):
    try:
        try:
            postType = class_name_to_class(__name__, post)
            if not is_post(postType):
                raise InvalidPostTypeError
        except Exception:
            raise InvalidPostTypeError
        foundPost = postType.objects.get(slug=slug)
        return str(foundPost.id)
    except DoesNotExist:
        return False
    except InvalidPostTypeError:
        raise InvalidPostTypeError()
    except Exception as e:
        raise e
Example #6
0
async def get_post_from_slug(post: str, slug: str):
    try:
        try:
            postType = class_name_to_class(__name__, post)
        except Exception:
            raise InvalidPostTypeError
        if not is_post(postType):
            raise InvalidPostTypeError
        post = postType.objects.get(slug=slug)
        return post.serialize()
    except DoesNotExist:
        raise NotFoundError().http_exception
    except InvalidPostTypeError:
        raise InvalidPostTypeError().http_exception
    except Exception as e:
        raise e
Example #7
0
async def get_post_schema(post: str,
                          identity: str = Depends(get_jwt_identity)):
    try:
        get_admin_user(identity)
        try:
            postType = class_name_to_class(__name__, post)
            if not is_post(postType):
                raise InvalidPostTypeError
        except Exception:
            raise InvalidPostTypeError
        return postType.schema()
    except (DoesNotExist, UnauthorizedError):
        raise UnauthorizedError('User is not admin')
    except InvalidPostTypeError:
        raise InvalidPostTypeError()
    except Exception as e:
        raise e
Example #8
0
async def delete_post(post: str,
                      id: str,
                      identity: str = Depends(get_jwt_identity)):
    try:
        get_admin_user(identity)
        try:
            postType = class_name_to_class(__name__, post)
            if not is_post(postType):
                raise InvalidPostTypeError
        except Exception:
            raise InvalidPostTypeError
        postType.objects.get(id=id).delete()
        return 'ok'
    except (DoesNotExist, UnauthorizedError):
        raise UnauthorizedError('User is not admin')
    except InvalidPostTypeError:
        raise InvalidPostTypeError()
    except Exception as e:
        raise e
Example #9
0
async def is_post_slug_taken(post: str,
                             slug: str,
                             identity: str = Depends(get_jwt_identity)):
    try:
        get_admin_user(identity)
    except (DoesNotExist, UnauthorizedError):
        raise UnauthorizedError('User is not admin')
    try:
        try:
            postType = class_name_to_class(__name__, post)
            if not is_post(postType):
                raise InvalidPostTypeError
        except Exception:
            raise InvalidPostTypeError
        postType.objects.get(slug=slug)
        return False
    except DoesNotExist:
        return True
    except InvalidPostTypeError:
        raise InvalidPostTypeError()
    except Exception as e:
        raise e
Example #10
0
async def update_post(id: str,
                      post_body: PostForm,
                      identity: str = Depends(get_jwt_identity)):
    try:
        get_admin_user(identity)
    except (DoesNotExist, UnauthorizedError):
        raise UnauthorizedError('User is not admin')
    try:
        try:
            postType = class_name_to_class(__name__, post_body.post)
            if not is_post(postType):
                raise InvalidPostTypeError
        except Exception:
            raise InvalidPostTypeError
        toUpdate = postType.objects.get(id=id)

        postTypeName = postType.__name__
        if postTypeName == 'Coupon':
            if post_body.obj['applicableProducts']:
                post_body.obj['applicableProducts'] = list(
                    map(lambda p: models.Product.objects.get(id=p),
                        post_body.obj['applicableProducts']))

        toUpdate.update(**post_body.obj)
        toUpdate.reload()
        toUpdate.modified = datetime.now
        toUpdate.generateNgrams()
        toUpdate.save()
        return 'ok'
    except DoesNotExist:
        raise NotFoundError()
    except InvalidPostTypeError:
        raise InvalidPostTypeError()
    except (FieldDoesNotExist, ValidationError, SchemaValidationError):
        raise SchemaValidationError()
    except Exception as e:
        raise e