def user_by_id_view(user_id): """Get user by id. --- get: tags: - Users security: - cookieAuth: [] responses: 200: content: application/json: schema: UserSchema 403: description: Forbidden 404: description: No such item 5XX: description: Unexpected error """ user = User.query.get_or_404(user_id) return success(UserSchema().dump(user))
def cities_list_view(page, limit, sort_by, query): """Get list of cities. --- get: tags: - Cities security: - cookieAuth: [] parameters: - in: query schema: FilterCitiesSchema responses: 200: content: application/json: schema: CityListSchema 403: description: Forbidden 400: content: application/json: schema: FailSchema 5XX: description: Unexpected error """ q = City.query if query: q = q.filter( City.name.ilike(f'%{query}%') ) total = q.count() q = q.order_by(sort_by).offset((page - 1) * limit).limit(limit) return success(CityListSchema().dump(dict( results=q, total=total )))
def devices_health_list_view(page, limit, sort_by, device_id, start_date_time, end_date_time): """Get list of devices health. --- get: tags: - System security: - cookieAuth: [] parameters: - in: query schema: FilterDeviceHealthSchema responses: 200: content: application/json: schema: DeviceHealthListSchema 400: content: application/json: schema: FailSchema 5XX: description: Unexpected error """ q = DeviceHealth.query if device_id: q = q.filter(DeviceHealth.device_id == device_id) if start_date_time: q = q.filter(DeviceHealth.created_at >= start_date_time) if end_date_time: q = q.filter(DeviceHealth.created_at <= end_date_time) total = q.count() q = q.order_by(sort_by).offset((page - 1) * limit).limit(limit) return success(DeviceHealthListSchema().dump(dict( results=q, total=total, )))
def put_attacks(user, upstream): """Sets a character's attack overrides. Must PUT a list of attacks.""" the_attacks = request.json # validation try: _validate_attacks(the_attacks) except ValidationError as e: return error(400, str(e)) # write response = current_app.mdb.characters.update_one( { "owner": user.id, "upstream": upstream }, {"$set": { "overrides.attacks": the_attacks }}) # respond if not response.matched_count: return error(404, "Character not found") return success("Attacks updated")
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} )
def logs_view(): """Save device logs file. --- post: tags: - System security: - tokenAuth: [] responses: 200: content: application/json: schema: type: string example: ok 400: content: application/json: schema: FailSchema 5XX: description: Unexpected error """ # TODO: save logs file somewhere # noqa return success('ok')
def delete_publisher_view(publisher_id): """Delete publisher. --- delete: tags: - Publishers security: - cookieAuth: [] responses: 200: content: application/json: schema: PublisherSchema 403: description: Forbidden 404: description: No such item 5XX: description: Unexpected error """ publisher = Publisher.query.get_or_404(publisher_id) db.session.delete(publisher) db.session.commit() return success(PublisherSchema().dump(publisher))
def register_device_view(uid_token): """Register new device. --- post: tags: - Devices requestBody: content: application/x-www-form-urlencoded: schema: RegisterDeviceSchema responses: 200: content: application/json: schema: RegisteredDeviceSchema 400: content: application/json: schema: FailSchema 5XX: description: Unexpected error """ device = save_device(uid_token=uid_token) return success(RegisteredDeviceSchema().dump(device))
def delete_city_view(city_id): """Delete city. --- delete: tags: - Cities security: - cookieAuth: [] responses: 200: content: application/json: schema: CitySchema 403: description: Forbidden 404: description: No such item 5XX: description: Unexpected error """ city = City.query.get_or_404(city_id) db.session.delete(city) db.session.commit() return success(CitySchema().dump(city))
def publishers_list_view(page, limit, sort_by, query, query_fields): """Get list of publishers. --- get: tags: - Publishers security: - cookieAuth: [] parameters: - in: query schema: FilterPublishersSchema responses: 200: content: application/json: schema: PublisherListSchema 403: description: Forbidden 400: content: application/json: schema: FailSchema 5XX: description: Unexpected error """ q = Publisher.query if query: q = q.filter( or_(*[ getattr(Publisher, f).ilike(f'%{query}%') for f in query_fields ])) total = q.count() q = q.order_by(sort_by).offset((page - 1) * limit).limit(limit) return success(PublisherListSchema().dump(dict(results=q, total=total)))
def delete_location_view(location_id): """Delete location. --- delete: tags: - Locations security: - cookieAuth: [] responses: 200: content: application/json: schema: LocationSchema 403: description: Forbidden 404: description: No such item 5XX: description: Unexpected error """ location = Location.query.get_or_404(location_id) db.session.delete(location) db.session.commit() return success(LocationSchema().dump(location))
def delete_device_view(device_id): """Delete device. --- delete: tags: - Devices security: - cookieAuth: [] responses: 200: content: application/json: schema: DeviceSchema 403: description: Forbidden 404: description: No such item 5XX: description: Unexpected error """ device = Device.query.get_or_404(device_id) db.session.delete(device) db.session.commit() return success(DeviceSchema().dump(device))
def get_guild_subscriptions(guild_id): """Returns a list of str representing the IDs of subscribed collections.""" return success([str(oid) for oid in WorkshopCollection.guild_active_ids(guild_id)], 200)
def get_guild_subscription(coll_id, guild_id): coll = WorkshopCollection.from_id(coll_id) sub = coll.guild_sub(guild_id) if sub is None: return error(404, "You are not subscribed to this collection") return success(sub, 200)
def guild_unsubscribe(user, coll_id, guild_id): guild_permissions_check(user, guild_id) coll = WorkshopCollection.from_id(coll_id) coll.unset_server_active(guild_id, int(user.id)) return success(f"Unsubscribed from {coll.name}", 200)
def get_personal_subscriptions(user): """Returns a list of str representing the IDs of subscribed collections.""" return success([str(oid) for oid in WorkshopCollection.my_sub_ids(int(user.id))], 200)
def moderator_delete_snippet_entitlement(_, body, snippet_id): snippet = WorkshopSnippet.from_id(snippet_id) result = _remove_entitlement_from_collectable(snippet, body['entity_type'], int(body['entity_id']), ignore_required=True) return success(result)
def moderator_delete_collection(_, coll_id): coll = WorkshopCollection.from_id(coll_id) coll.delete(run_checks=False) return success(f"Deleted {coll.name}", 200)
def get_editable_collections(user): """Returns a list of collection IDs the user has edit permission on in an unsorted order.""" editable = WorkshopCollection.my_editable_ids(int(user.id)) return success(list([str(o) for o in editable]), 200)
def get_tags(): tags = current_app.mdb.workshop_tags.find() return success(list(tags), 200)
def get_owned_collections(user): """Returns a list of collection IDs the user owns in an unsorted order.""" owned = WorkshopCollection.user_owned_ids(int(user.id)) return success([str(o) for o in owned], 200)
def moderator_delete_alias_entitlement(_, body, alias_id): alias = WorkshopAlias.from_id(alias_id) result = _remove_entitlement_from_collectable(alias, body['entity_type'], int(body['entity_id']), ignore_required=True) return success(result)
def create_collection(user, body): new_collection = WorkshopCollection.create_new(int(user.id), body['name'], body['description'], body['image']) return success(new_collection.to_dict(js=True), 201)
def add_snippet_entitlement(user, body, snippet_id): snippet = get_collectable_with_editor_check(WorkshopSnippet, snippet_id, user) return success(_add_entitlement_to_collectable(snippet, body['entity_type'], int(body['entity_id'])))
def moderator_add_alias_entitlement(_, body, alias_id): alias = WorkshopAlias.from_id(alias_id) return success(_add_entitlement_to_collectable(alias, body['entity_type'], int(body['entity_id']), required=True))
def delete_snippet_entitlement(user, body, snippet_id): snippet = get_collectable_with_editor_check(WorkshopSnippet, snippet_id, user) return success(_remove_entitlement_from_collectable(snippet, body['entity_type'], int(body['entity_id'])))
def moderator_add_snippet_entitlement(_, body, snippet_id): snippet = WorkshopSnippet.from_id(snippet_id) return success(_add_entitlement_to_collectable(snippet, body['entity_type'], int(body['entity_id']), required=True))
def personal_unsubscribe(user, coll_id): coll = WorkshopCollection.from_id(coll_id) coll.unsubscribe(int(user.id)) return success(f"Unsubscribed from {coll.name}", 200)
def get_collection(user, coll_id): coll = get_collection_with_private_check(coll_id, user) return success(coll.to_dict(js=True), 200)
def get_personal_subscription(user, coll_id): coll = WorkshopCollection.from_id(coll_id) sub = coll.my_sub(int(user.id)) if sub is None: return error(404, "You are not subscribed to this collection") return success(sub, 200)