From 70dc0a21755fa870f878071fc4ca5bdded0468b4 Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Thu, 8 Oct 2015 09:24:34 +0200 Subject: [PATCH] Minor refactoring of authentication functions --- pillar/application/__init__.py | 69 +++++++++++------------ pillar/application/file_server.py | 91 ------------------------------- 2 files changed, 35 insertions(+), 125 deletions(-) delete mode 100644 pillar/application/file_server.py diff --git a/pillar/application/__init__.py b/pillar/application/__init__.py index 4da99093..a30ffbce 100644 --- a/pillar/application/__init__.py +++ b/pillar/application/__init__.py @@ -1,5 +1,6 @@ import os import json +import requests from eve import Eve from pymongo import MongoClient @@ -48,7 +49,7 @@ class SystemUtility(): def validate(token): """Validate a Token against Blender ID server """ - import requests + payload = dict( token=token) try: @@ -159,40 +160,40 @@ class CustomTokenAuth(BasicsAuth): def authorized_protected(self): pass -def convert_properties(properties, node_schema): - for prop in node_schema: - if not prop in properties: - continue - schema_prop = node_schema[prop] - prop_type = schema_prop['type'] - if prop_type == 'dict': - properties[prop] = convert_properties( - properties[prop], schema_prop['schema']) - if prop_type == 'list': - if properties[prop] in ['', '[]']: - properties[prop] = [] - for k, val in enumerate(properties[prop]): - if not 'schema' in schema_prop: - continue - item_schema = {'item': schema_prop['schema']} - item_prop = {'item': properties[prop][k]} - properties[prop][k] = convert_properties( - item_prop, item_schema)['item'] - # Convert datetime string to RFC1123 datetime - elif prop_type == 'datetime': - prop_val = properties[prop] - properties[prop] = datetime.strptime(prop_val, RFC1123_DATE_FORMAT) - elif prop_type == 'objectid': - prop_val = properties[prop] - if prop_val: - properties[prop] = ObjectId(prop_val) - else: - properties[prop] = None - - return properties - class ValidateCustomFields(Validator): + def convert_properties(properties, node_schema): + for prop in node_schema: + if not prop in properties: + continue + schema_prop = node_schema[prop] + prop_type = schema_prop['type'] + if prop_type == 'dict': + properties[prop] = convert_properties( + properties[prop], schema_prop['schema']) + if prop_type == 'list': + if properties[prop] in ['', '[]']: + properties[prop] = [] + for k, val in enumerate(properties[prop]): + if not 'schema' in schema_prop: + continue + item_schema = {'item': schema_prop['schema']} + item_prop = {'item': properties[prop][k]} + properties[prop][k] = convert_properties( + item_prop, item_schema)['item'] + # Convert datetime string to RFC1123 datetime + elif prop_type == 'datetime': + prop_val = properties[prop] + properties[prop] = datetime.strptime(prop_val, RFC1123_DATE_FORMAT) + elif prop_type == 'objectid': + prop_val = properties[prop] + if prop_val: + properties[prop] = ObjectId(prop_val) + else: + properties[prop] = None + + return properties + def _validate_valid_properties(self, valid_properties, field, value): node_types = app.data.driver.db['node_types'] lookup = {} @@ -200,7 +201,7 @@ class ValidateCustomFields(Validator): node_type = node_types.find_one(lookup) try: - value = convert_properties(value, node_type['dyn_schema']) + value = self.convert_properties(value, node_type['dyn_schema']) except Exception, e: print ("Error converting: {0}".format(e)) #print (value) diff --git a/pillar/application/file_server.py b/pillar/application/file_server.py deleted file mode 100644 index 53fc6696..00000000 --- a/pillar/application/file_server.py +++ /dev/null @@ -1,91 +0,0 @@ -import os -import hashlib -from datetime import datetime -from PIL import Image -from bson import ObjectId -from flask import Blueprint -from flask import request -from application import app -from application import db -from application import post_item -from application.utils.imaging import generate_local_thumbnails - - -RFC1123_DATE_FORMAT = '%a, %d %b %Y %H:%M:%S GMT' - - -file_server = Blueprint('file_server', __name__, - template_folder='templates', - static_folder='static/storage') - - -def hashfile(afile, hasher, blocksize=65536): - buf = afile.read(blocksize) - while len(buf) > 0: - hasher.update(buf) - buf = afile.read(blocksize) - return hasher.hexdigest() - - -@file_server.route('/build_thumbnails/') -def build_thumbnails(file_path): - # Search file with backend "pillar" and path=file_path - file_ = db.files.find({"path": "{0}".format(file_path)}) - file_ = file_[0] - user = file_['user'] - - file_full_path = os.path.join(app.config['FILE_STORAGE'],file_path) - # Does the original file exist? - if not os.path.isfile(file_full_path): - return "", 404 - else: - thumbnails = generate_local_thumbnails(file_full_path, - return_image_stats=True) - - for size, thumbnail in thumbnails.iteritems(): - if thumbnail.get('exists'): - # If a thumbnail was already made, we just continue - continue - basename = os.path.basename(thumbnail['path']) - root, ext = os.path.splitext(basename) - path = os.path.join(basename[:2], basename) - file_object = dict( - name=root, - description="Preview of file {0}".format(file_['name']), - user=user, - parent=file_['_id'], - size=size, - format=ext[1:], - width=thumbnail['width'], - height=thumbnail['height'], - content_type=thumbnail['content_type'], - length=thumbnail['length'], - md5=thumbnail['md5'], - filename=basename, - backend='pillar', - path=path) - # Commit to database - r = post_item('files', file_object) - if r[0]['_status'] == 'ERR': - return "", r[3] # The error code from the request - - return "", 200 - - -@file_server.route('/file', methods=['POST']) -@file_server.route('/file/') -def index(file_name=None): - #GET file - if file_name: - return file_server.send_static_file(file_name) - #POST file - file_name = request.form['name'] - folder_name = file_name[:2] - file_folder_path = os.path.join(app.config['FILE_STORAGE'], - folder_name) - if not os.path.exists(file_folder_path): - os.mkdir(file_folder_path) - file_path = os.path.join(file_folder_path, file_name) - request.files['data'].save(file_path) - - return "{}", 200