import os import logging import datetime from sqlalchemy.orm.exc import NoResultFound from flask import Flask, request, session, g, redirect, abort from flask_restful import Resource from puerta.app import db, api, app from puerta.models import Unit log = logging.getLogger(__name__) class UnitsEndpoint(Resource): def get(self): return [{"id": unit.id, "name": unit.name} for unit in db.session.query(Unit).all()] api.add_resource(UnitsEndpoint, "/api/v1/unit")
''' Authenticate... ''' j = request.get_json() try: user = db.session.query(User).filter(and_( User.email == j.get('email'), User.approved == True )).one() except NoResultFound: abort(404) if user.check_password(j.get('pass')): session['user_id'] = user.id return { 'id': user.id, 'fullname': user.fullname, 'role': user.role } abort(401) def delete(self): ''' Remove authentication... ''' session.pop('user_id', None) return {'success': True} api.add_resource(AuthEndpoint, '/api/v1/auth')
abort(403) if photo in user.likes: user.likes.remove(photo) liked = False else: user.likes.append(photo) liked = True db.session.commit() return {'success': True, 'likes_count': photo.user_likes.count(), 'liked': liked} class PhotoCommentEndpoint(Resource): def get(self, id): ''' id is the photo id... ''' photo = db.session.query(PhotoFile).get(id) if photo is None: abort(404) return { 'photo_id': photo.id, 'comments': [{ 'id': comment.id, 'comment': comment.comment } for comment in photo.comments] } api.add_resource(PhotoCommentEndpoint, '/api/v1/album/photo/<int:id>/comment') api.add_resource(PhotoEndpoint, '/api/v1/album/photo/<int:id>')
import logging import datetime from sqlalchemy.orm.exc import NoResultFound from flask import Flask, request, session, g, redirect, abort from flask_restful import Resource from puerta.app import db, api, app from puerta.models import User, Child log = logging.getLogger(__name__) class RegisterEndpoint(Resource): def post(self): ''' Register a new user ''' j = request.get_json() user = User(j.get('fullname'), j.get('email'), j.get('password'), j.get('subscribed')) db.session.add(user) for child in j.get('children'): dbChild = Child(child.get('fullname')) dbChild.unit_id = child.get('unitId') user.children.append(dbChild) db.session.commit() return {'success': True, 'id': user.id} api.add_resource(RegisterEndpoint, '/api/v1/register')
user = db.session.query(User).get(id) if user is None: abort(404) user.approved = not user.approved if user.approved: current_user = db.session.query(User).get(session['user_id']) if current_user: user.who_approved = current_user.fullname else: abort(403) db.session.commit() return { 'success': True, 'id': id} def put(self, id): user = db.session.query(User).get(id) if user is None: abort(404) j = request.get_json() user.role = j.get('role') db.session.commit() return { 'success': True, 'id': id} api.add_resource(UsersEndpoint, '/api/v1/user') api.add_resource(UserEndpoint, '/api/v1/user/<int:id>')