Ejemplo n.º 1
0
    def get(self, id=None):
        role = g.user.role
        role_dict = role.as_dict()
        if id == g.user.role_id and app.config['ADMIN_ROLE'] != g.user.role.name:
            return jsonify(role_dict)

        role.name == app.config['ADMIN_ROLE'] or abort(403)
        users = User.query.filter_by(role_id=role.id).all()
        try:
            page_size = t.Dict({'page_size': t.Int}).check(
                request.json)['page_size']
        except t.DataError:
            page_size = app.config['DEFAULT_PAGE_SIZE']

        role_dict.update({'total': len(users), 'users_id': users[:page_size]})
        return jsonify(role_dict)
Ejemplo n.º 2
0
    def delete(self, id):
        uid = self.current_user or abort(404)
        try:
            Address.query.filter_by(id=id, user_id=uid).delete()
            data, status = {}, 200
        except t.DataError as e:
            data, status = e.as_dict(), 400

        return jsonify(data, status=status)
Ejemplo n.º 3
0
    def delete(self, id):
        uid = _creator()
        try:
            Product.query.filter_by(id=id, created_by=uid).delete()
            data, status = {}, 200
        except t.DataError as e:
            data, status = e.as_dict(), 400

        return jsonify(data, status=status)
Ejemplo n.º 4
0
 def get(self, id=None):
     uid = self.current_user or abort(401)
     if id is None:
         addresses = Address.query.filter_by(user_id=uid)
         response = [addr.as_dict() for addr in addresses]
     else:
         addr = Address.query.filter_by(id=id, user_id=uid).first_or_404()
         response = addr.as_dict()
     return jsonify(response)
Ejemplo n.º 5
0
 def put(self, id):
     uid = _creator()
     data = request.json or abort(400)
     data.update({'created_by': uid})
     try:
         product = Product.query.get_or_404(id)
         product.update(**data)
         data, status = product.as_dict(), 201
     except t.DataError as e:
         data, status = e.as_dict(), 400
     return jsonify(data, status=status)
Ejemplo n.º 6
0
    def post(self):
        uid = self.current_user or abort(401)
        data = request.json or abort(400)
        data.update({'user_id': uid})
        try:
            addr = Address.create(**self.validation.check(data))
            data, status = addr.as_dict(), 201
        except t.DataError as e:
            data, status = e.as_dict(), 400

        return jsonify(data, status=status)
Ejemplo n.º 7
0
    def put(self, id):
        user = g.user
        if 'token' in request.json:
            user = User.validate_token(request.json['token'])
        try:
            user.update(**self.validation.check(request.json))
            response, status = user.as_dict(), 202
        except t.DataError as e:
            response, status = e.as_dict(), 400

        return jsonify(response, status=status)
Ejemplo n.º 8
0
    def put(self, id):
        data, status = request.json or abort(400), 202
        validation = t.Dict({'email': t.Email, 'password':
            t.String}).ignore_extra('*').append(self._authenticate)
        try:
            validation.check(data)
            response = dict(session)
        except t.DataError as e:
            response, status = e.as_dict(), 404
            session.update({'is_anonymous': True})

        return jsonify(response, status=status)
Ejemplo n.º 9
0
 def get(self, id=None):
     uid = _creator()
     if id is None:
         products = Product.query.filter_by(created_by=uid)
         response = [(one_pro.slug, one_pro.as_dict())\
                     for one_pro in products]
         response = dict(response)
     else:
         product = Product.query.filter_by(
             id=id, created_by=uid).first_or_404()
         response = product.as_dict()
     return jsonify(response)
Ejemplo n.º 10
0
 def put(self, id):
     uid = self.current_user or abort(403)
     data = request.json or abort(400)
     data.update({'user_id': uid})
     self.validation.make_optional('apartment', 'zip_code', 'user_id')
     try:
         addr = Address.query.get_or_404(id)
         addr.update(**data)
         data, status = addr.as_dict(), 201
     except t.DataError as e:
         data, status = e.as_dict(), 400
     return jsonify(data, status=status)
Ejemplo n.º 11
0
 def post(self):
     data = request.json or abort(400)
     try:
         data = self.validation.check(data)
         if User.is_unique(data['email']):
             user = User.create(email=data['email'])
             response, status = dict(session), 201
             response['uid'] = user.id
         else:
             response, status = {'email': "This email is already taken"}, 400
     except t.DataError as e:
         response, status = e.as_dict(), 400
     return jsonify(response, status=status)
Ejemplo n.º 12
0
    def put(self, id):
        g.user.role.name == app.config['ADMIN_ROLE'] or abort(403)
        try:
            data = t.Dict({'uid': t.Int, 'role_id': t.Int}).check(
                request.json)
            status = 201
        except t.DataError as e:
            data, status = e.as_dict(), 400

        if status == 201:
            data = User.get(data['uid']).update(
                role_id=data['role_id']).as_dict()

        return jsonify(data, status=status)
Ejemplo n.º 13
0
 def delete(self, id):
     id == g.user.id or abort(403)
     g.user.delete()
     return jsonify({}, status=200)
Ejemplo n.º 14
0
 def post(self):
     user = User.validate_token(request.json.get('token'))
     return jsonify(user.as_dict(), status=200)
Ejemplo n.º 15
0
 def get(self, id=None):
     id == g.user.id or abort(403)
     return jsonify(g.user.as_dict())
Ejemplo n.º 16
0
 def delete(self, id):
     session['is_anonymous'] = True
     del session['uid']
     return jsonify(dict(session), status=200)
Ejemplo n.º 17
0
 def get(self, id=None):
     session.update({'is_anonymous': not bool(self.current_user),
                     'id': session.get('id', uuid.uuid4().hex)})
     return jsonify(dict(session))