コード例 #1
0
ファイル: unit.py プロジェクト: IndiciumSRL/puerta-server
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")
コード例 #2
0
ファイル: auth.py プロジェクト: IndiciumSRL/puerta-server
        '''
        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')
コード例 #3
0
ファイル: photo.py プロジェクト: IndiciumSRL/puerta-server
            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>')
コード例 #4
0
ファイル: register.py プロジェクト: IndiciumSRL/puerta-server
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')
コード例 #5
0
ファイル: users.py プロジェクト: IndiciumSRL/puerta-server
        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>')