Exemplo n.º 1
0
 def put_detail(self, collection, pk):
     """Updates whole document."""
     collection = self.get_collection(collection)
     if '_id' in request.json:
         # we are ignoring id fields of bundle,
         # because _id field is immutable
         del request.json['_id']
     collection.update({"_id": ObjectId(pk)}, request.json)
     response.status = 202
     return jsonify(request.json)
Exemplo n.º 2
0
 def put_detail(self, collection, pk):
     """Updates whole document."""
     collection = self.get_collection(collection)
     if '_id' in request.json:
         # we are ignoring id fields of bundle,
         # because _id field is immutable
         del request.json['_id']
     collection.update({"_id": ObjectId(pk)}, request.json)
     response.status = 202
     return jsonify(request.json)
Exemplo n.º 3
0
 def authenticate(self):
     collection = self.connection['users']
     username = request.json.get('username')
     password = request.json.get('password')
     hasher = hashlib.md5()
     hasher.update(password)
     user = collection.find_one({
         'username': username,
         'password': hasher.hexdigest(),
     }) or abort(400, 'Wrong username or password')
     access_token = str(uuid.uuid4())
     self.connection['access_tokens'].insert({
         'access_token': access_token,
         'user_id': user.get('id')})
     user.pop('password')
     user.update({'access_token': access_token})
     return jsonify(user)
Exemplo n.º 4
0
 def authenticate(self):
     collection = self.connection['users']
     username = request.json.get('username')
     password = request.json.get('password')
     hasher = hashlib.md5()
     hasher.update(password)
     user = collection.find_one({
         'username': username,
         'password': hasher.hexdigest(),
     }) or abort(400, 'Wrong username or password')
     access_token = str(uuid.uuid4())
     self.connection['access_tokens'].insert({
         'access_token': access_token,
         'user_id': user.get('id')
     })
     user.pop('password')
     user.update({'access_token': access_token})
     return jsonify(user)
Exemplo n.º 5
0
    def get_list(self, collection):
        """Returns paginated objects."""
        collection = self.get_collection(collection)
        limit = int_or_default(request.query.limit, 20)
        offset = int_or_default(request.query.offset, 0)
        query = self.get_query()
        cursor = collection.find(query)

        meta = {
            "limit": limit,
            "offset": offset,
            "total_count": cursor.count(),
        }

        objects = cursor.skip(offset).limit(limit)
        objects = map(self.get_bundler(collection), objects)

        return jsonify({"meta": meta, "objects": objects})
Exemplo n.º 6
0
 def register(self):
     collection = self.connection['users']
     password = request.json.get('password')
     username = request.json.get('username')
     email = request.json.get('email')
     if not username or not password:
         abort(400, 'Please give an username and password')
     if collection.find_one({'username': username}):
         abort(400, 'A user with that username already exists')
     if collection.find_one({'email': email}):
         abort(400, 'A user with that email already exists')
     hasher = hashlib.md5()
     hasher.update(password)
     response.status = 201
     return jsonify({"_id": collection.insert({
         'username': request.json.get('username'),
         'email': request.json.get('email'),
         'password': hasher.hexdigest()
     })})
Exemplo n.º 7
0
    def get_list(self, collection):
        """Returns paginated objects."""
        collection = self.get_collection(collection)
        limit = int_or_default(request.query.limit, 20)
        offset = int_or_default(request.query.offset, 0)
        query = self.get_query()
        cursor = collection.find(query)

        meta = {
            "limit": limit,
            "offset": offset,
            "total_count": cursor.count(),
        }

        objects = cursor.skip(offset).limit(limit)
        objects = map(self.get_bundler(collection), objects)

        return jsonify({"meta": meta,
                        "objects": objects})
Exemplo n.º 8
0
 def register(self):
     collection = self.connection['users']
     password = request.json.get('password')
     username = request.json.get('username')
     email = request.json.get('email')
     if not username or not password:
         abort(400, 'Please give an username and password')
     if collection.find_one({'username': username}):
         abort(400, 'A user with that username already exists')
     if collection.find_one({'email': email}):
         abort(400, 'A user with that email already exists')
     hasher = hashlib.md5()
     hasher.update(password)
     response.status = 201
     return jsonify({
         "_id":
         collection.insert({
             'username': request.json.get('username'),
             'email': request.json.get('email'),
             'password': hasher.hexdigest()
         })
     })
Exemplo n.º 9
0
 def error(self, error, message=None):
     """Returns the error response."""
     return jsonify({"error": error.status_code,
                     "message": error.body or message})
Exemplo n.º 10
0
 def post_list(self, collection):
     """Creates new document"""
     collection = self.get_collection(collection)
     inserted = collection.insert(request.json)
     response.status = 201
     return jsonify({"_id": inserted})
Exemplo n.º 11
0
 def get_detail(self, collection, pk):
     """Returns a single document."""
     cursor = self.get_collection(collection)
     data = cursor.find_one({"_id": ObjectId(pk)}) or abort(404)
     return jsonify(self.get_bundler(cursor)(data))
Exemplo n.º 12
0
 def error(self, error, message=None):
     """Returns the error response."""
     return jsonify({
         "error": error.status_code,
         "message": error.body or message
     })
Exemplo n.º 13
0
 def post_list(self, collection):
     """Creates new document"""
     collection = self.get_collection(collection)
     inserted = collection.insert(request.json)
     response.status = 201
     return jsonify({"_id": inserted})
Exemplo n.º 14
0
 def get_detail(self, collection, pk):
     """Returns a single document."""
     cursor = self.get_collection(collection)
     data = cursor.find_one({"_id": ObjectId(pk)}) or abort(404)
     return jsonify(self.get_bundler(cursor)(data))