Beispiel #1
0
def update_file_view(file_id, **kwargs):
    """Update file.
    ---
    put:
      tags:
        - Content
      security:
        - cookieAuth: []
      requestBody:
        content:
          multipart/form-data:
            schema: UpdateFileSchema
      responses:
        200:
          content:
            application/json:
              schema: FileSchema
        400:
          content:
            application/json:
              schema: FailSchema
        403:
          description: Forbidden
        404:
          description: No such item
        5XX:
          description: Unexpected error
    """
    file = File.query.get_or_404(file_id)
    if file.created_by != current_user.id and not current_user.is_admin:
        return fail('You can not edit this item', 403)
    file = save_content(instance=file, **kwargs)
    return success(FileSchema().dump(file))
Beispiel #2
0
def add_device_health_view(**kwargs):
    """Add device health
    ---
    post:
      tags:
        - System
      security:
        - tokenAuth: []
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema: AddDeviceHealthSchema
      responses:
        200:
          content:
            application/json:
              schema: DeviceHealthSchema
        400:
          content:
            application/json:
              schema: FailSchema
        5XX:
          description: Unexpected error
    """
    try:
        device_health = save_device_health(device_id=current_device.id, **kwargs)
    except DeviceHealthException as e:
        return fail(str(e))

    return success(DeviceHealthSchema().dump(device_health))
Beispiel #3
0
def delete_file_view(file_id):
    """Delete file.
    ---
    delete:
      tags:
        - Content
      security:
        - cookieAuth: []
      responses:
        200:
          content:
            application/json:
              schema: FileSchema
        403:
          description: Forbidden
        404:
          description: No such item
        5XX:
          description: Unexpected error
    """
    file = File.query.get_or_404(file_id)
    if file.created_by != current_user.id and not current_user.is_admin:
        return fail('You can not delete this item', 403)
    response = FileSchema().dump(file)
    db.session.delete(file)
    db.session.commit()
    delete_file(file.src)
    return success(response)
Beispiel #4
0
def update_city_view(city_id, **kwargs):
    """Update city.
    ---
    put:
      tags:
        - Cities
      security:
        - cookieAuth: []
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema: UpdateCitySchema
      responses:
        200:
          content:
            application/json:
              schema: CitySchema
        400:
          content:
            application/json:
              schema: FailSchema
        403:
          description: Forbidden
        404:
          description: No such item
        5XX:
          description: Unexpected error
    """
    try:
        city = save_city(
            instance=City.query.get_or_404(city_id),
            **kwargs)
    except CityException as e:
        return fail(str(e))
    return success(CitySchema().dump(city))
Beispiel #5
0
def add_publisher_view(**kwargs):
    """Add publisher.
    ---
    post:
      tags:
        - Publishers
      security:
        - cookieAuth: []
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema: AddPublisherSchema
      responses:
        200:
          content:
            application/json:
              schema: PublisherSchema
        400:
          content:
            application/json:
              schema: FailSchema
        403:
          description: Forbidden
        5XX:
          description: Unexpected error
    """
    try:
        publisher = save_publisher(created_by=current_user.id, **kwargs)
    except PublisherException as e:
        return fail(str(e))

    return success(PublisherSchema().dump(publisher))
Beispiel #6
0
def add_city_view(**kwargs):
    """Add new city.
    ---
    post:
      tags:
        - Cities
      security:
        - cookieAuth: []
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema: AddCitySchema
      responses:
        200:
          content:
            application/json:
              schema: CitySchema
        400:
          content:
            application/json:
              schema: FailSchema
        5XX:
          description: Unexpected error
    """
    try:
        city = save_city(**kwargs)
    except CityException as e:
        return fail(str(e))
    return success(CitySchema().dump(city))
Beispiel #7
0
def update_publisher_view(publisher_id, **kwargs):
    """Update publisher.
    ---
    put:
      tags:
        - Publishers
      security:
        - cookieAuth: []
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema: UpdatePublisherSchema
      responses:
        200:
          content:
            application/json:
              schema: PublisherSchema
        400:
          content:
            application/json:
              schema: FailSchema
        403:
          description: Forbidden
        404:
          description: No such item
        5XX:
          description: Unexpected error
    """
    publisher = Publisher.query.get_or_404(publisher_id)
    try:
        publisher = save_publisher(publisher, **kwargs)
    except PublisherException as e:
        return fail(str(e))

    return success(PublisherSchema().dump(publisher))
Beispiel #8
0
def add_user_view(**kwargs):
    """Add user.
    ---
    post:
      tags:
        - Users
      security:
        - cookieAuth: []
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema: AddUserSchema
      responses:
        200:
          content:
            application/json:
              schema: UserSchema
        400:
          content:
            application/json:
              schema: FailSchema
        403:
          description: Forbidden
        5XX:
          description: Unexpected error
    """
    try:
        user = save_user(**kwargs)
    except UserException as e:
        return fail(str(e))

    return success(UserSchema().dump(user))
Beispiel #9
0
def login_user_view(email, password):
    """Login user.
    ---
    post:
      tags:
        - Auth
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema: LoginUserSchema
      responses:
        200:
          content:
            application/json:
              schema: UserSchema
          headers:
            Set-Cookie:
              description:
                Contains the session cookie named from env var `AUTH_COOKIE_NAME`.
                Pass this cookie back in subsequent requests.
              schema:
                type: string
        400:
          content:
            application/json:
              schema: FailSchema
        5XX:
          description: Unexpected error
    """
    try:
        user, sid = login_user(email=email, password=password)
    except UserException as e:
        return fail(str(e))
    return success(
        data=UserSchema().dump(user),
        cookies={app.config.get('AUTH_COOKIE_NAME'): sid}
    )
Beispiel #10
0
 def handle_error(self, error, req, schema, error_status_code,
                  error_headers):
     response = fail(
         [f"Field:{k}. {' '.join(v)}" for k, v in error.messages.items()])
     abort(response)
Beispiel #11
0
 def wrapped(*args, **kwargs):
     if not current_device:
         return fail('Доступ запрещен', status=403)
     return fn(*args, **kwargs)
Beispiel #12
0
 def wrapped(*args, **kwargs):
     if not current_user or roles and current_user.role not in roles:
         return fail('Доступ запрещен', status=403)
     return fn(*args, **kwargs)