def add_template(session_id): user_id = session.get_user_id(session_id) brand_id = request.args.get('brand_id') if brand_id == None: return 500, 'Empty brand!' user_brands = people.get_user_items("select * from (" + consts.SQL_GET_USER_BRANDS + ") where ID = :brand_id", user_id, None, brand_id=brand_id) if len(user_brands) == 0: return 500, 'Access denied!' params = json.loads(request.data) template_name = params["template_name"] path = "company_%03d" % user_brands[0]["COMPANY_ID"] + "/brand_%04d" % int(brand_id) path = path + "/" + template_name.translate(tr).replace(" ","_") main_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) + "/templates/" directory = main_path + path print directory if not os.path.exists(directory): os.makedirs(directory+"/files") shutil.copyfile(main_path + "empty_template.py", main_path + path + "/template.py") shutil.copyfile(main_path + "preview.jpg", main_path + path + "/files/preview.jpg") f = file(main_path + path + "/__init__.py", 'w') f.close() template.insert_table_template(template_name, brand_id, path) res = {} res['templates'] = people.get_user_items(consts.SQL_GET_USER_TEMPLATES, user_id, None) return jsonify(res) else: print "!" return "Name is busy!", 500
def get_admin_companies(session_id, role): user_id = session.get_user_id(session_id) user_role = None if role == "user" else role res = {} res['companies'] = people.get_user_items(consts.SQL_GET_USER_COMPANIES, user_id, user_role) return jsonify(res)
def generate_template_preview(template_id, session_id): user_id = session.get_user_id(session_id) print user_id, template_id user_templates = people.get_user_items("select * from (" + SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) params = json.loads(request.data) template_request = parse_template_request(params) if len(user_templates) == 0: print "Access denied" return "Access denied", 500 user_template = user_templates[0] template_request.height = int(template_request.height * 640 / template_request.width) template_request.width = 640 file_name = tasks.generate_picture(user_template["PATH"], template_request.width, template_request.height, "JPEG", float(96), user_id=user_id, preview=True) return os.path.basename(file_name), 200
def save_template(session_id): user_id = session.get_user_id(session_id) params = json.loads(request.data) template_id = params["id"] user_templates = people.get_user_items( "select * from (" + consts.SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) if len(user_templates) == 0: return "Access denied", 500 user_template = user_templates[0] # params = json.loads(request.data) template_code = params["code"].encode("utf-8") template_name = params["name"] template_project = params["project"] main_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) file = open(main_path + '/templates/%s/template.py' % user_template['PATH'], 'w+') # res = [{"name": o} for o in os.listdir(d) if os.path.isdir(os.path.join(d, o))] file.write(template_code) file.close() template.update_table_template(template_id, NAME=template_name, PROJECT=template_project) res = {} # res['templates'] = people.get_user_items(consts.SQL_GET_USER_TEMPLATES, user_id, None) return "OK"
def generate_async_template(template_id, session_id): user_id = session.get_user_id(session_id) print user_id, template_id user_templates = people.get_user_items("select * from (" + SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) params = json.loads(request.data) template_request = parse_template_request(params) if len(user_templates) == 0: return "Access denied", 500 user_template = user_templates[0] process_task = tasks.generate_picture.apply_async( (user_template["PATH"], template_request.width, template_request.height, template_request.format, float(template_request.dpi), user_id), queue='TEMPLATE.Q') task_id = process_task.id task_info = "%s, %sx%s dpi:%s, format: %s" % ( user_template["NAME"], template_request.width, template_request.height, template_request.dpi, template_request.format) task.insert_table_task(task_id, user_id, task_info) return jsonify(task.get_user_tasks(int(user_id))), 200
def delete_template(session_id): user_id = session.get_user_id(session_id) params = json.loads(request.data) template_id = params["template_id"] user_templates = people.get_user_items( "select * from (" + consts.SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) if len(user_templates) == 0: return "Access denied", 500 user_template = user_templates[0] template.delete_template(template_id) path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) + "/templates/" + user_template["PATH"] shutil.rmtree(path) res = {} res['templates'] = people.get_user_items(consts.SQL_GET_USER_TEMPLATES, user_id, None) return jsonify(res)
def get_list_of_brands(user_id, user_role, archived): res = {} res['brands'] = people.get_user_items( "select * from (" + consts.SQL_GET_USER_BRANDS + ") where (ARCHIVED = 0 or ARCHIVED = :archived)", user_id, user_role, archived=archived) return res
def check_rule_access(user_id, source, item_id): user_items = [] if source == 'template': user_items = people.get_user_items("select * from (" + consts.SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=item_id) elif source == 'brand': user_items = people.get_user_items("select * from (" + consts.SQL_GET_USER_BRANDS + ") where ID = :brand_id", user_id, None, brand_id=item_id) elif source == 'company': user_items = people.get_user_items("select * from (" + consts.SQL_GET_USER_COMPANIES + ") where ID = :company_id", user_id, None, company_id=item_id) return len(user_items) > 0
def template_rule_list(session_id): template_id = request.args.get('template_id') user_id = session.get_user_id(session_id) if template_id == None: return 500, 'Empty template!' user_brands = people.get_user_items("select * from (" + consts.SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) if len(user_brands) == 0: return 500, 'Access denied!' res = {} res["users"] = rules.get_template_rules(template_id) return jsonify(res)
def company_rule_list(session_id): company_id = request.args.get('company_id') user_id = session.get_user_id(session_id) if company_id == None: return 500, 'Empty company!' user_companies = people.get_user_items("select * from (" + consts.SQL_GET_USER_COMPANIES + ") where ID = :company_id", user_id, None, company_id=company_id) if len(user_companies) == 0: return 500, 'Access denied!' res = {} res["users"] = rules.get_company_rules(company_id) return jsonify(res)
def save_brand(session_id): params = json.loads(request.data) brand_id = params["brand_id"] brand_name = params["brand_name"] user_id = session.get_user_id(session_id) user_brands = people.get_user_items("select * from (" + consts.SQL_GET_USER_BRANDS + ") where ID = :brand_id", user_id, "ADMIN", brand_id=brand_id) if len(user_brands) == 0: return 500, 'Access denied!' brand.update_table_brand(brand_id, NAME=brand_name) return "OK"
def brand_rule_list(session_id): brand_id = request.args.get('brand_id') user_id = session.get_user_id(session_id) if brand_id == None: return 500, 'Empty brand!' user_brands = people.get_user_items("select * from (" + consts.SQL_GET_USER_BRANDS + ") where ID = :brand_id", user_id, None, brand_id=brand_id) if len(user_brands) == 0: return 500, 'Access denied!' res = {} res["users"] = rules.get_brand_rules(brand_id) return jsonify(res)
def get_template_code(session_id): user_id = session.get_user_id(session_id) template_id = request.args.get("template_id") if template_id == None: return 500 user_templates = people.get_user_items("select * from (" + SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) if len(user_templates) == 0: return "Access denied", 500 template = user_templates[0] file_path = os.path.abspath(os.path.dirname( __file__)) + '/templates/%s/template.py' % template["PATH"] file = open(file_path) # res = [{"name": o} for o in os.listdir(d) if os.path.isdir(os.path.join(d, o))] return file.read()
def get_preview(template_id, session_id): mimetypes = {".png": "image/png", ".jpg": "image/jpeg"} user_id = session.get_user_id(session_id) user_templates = people.get_user_items("select * from (" + SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) if len(user_templates) == 0: return "Access denied", 500 template = user_templates[0] mimetype = mimetypes.get(".jpg", "text/html") file_path = os.path.abspath(os.path.dirname( __file__)) + '/templates/%s/files/preview.jpg' % template["PATH"] return send_file(file_path, mimetype, attachment_filename=file_path, cache_timeout=0), 200
def add_brand(session_id): company_id = request.args.get('company_id') archived = request.args.get('archived') user_id = session.get_user_id(session_id) if company_id is None: return 500, 'Empty brand!' user_companies = people.get_user_items("select * from (" + consts.SQL_GET_USER_COMPANIES + ") where ID = :company_id", user_id, "ADMIN", company_id=company_id) if len(user_companies) == 0: return 500, 'Access denied!' if archived is None: archived = 0 params = json.loads(request.data) brand_name = params["brand_name"] brand.insert_table_brand(brand_name, company_id) return jsonify(get_list_of_brands(user_id, "ADMIN", archived))
def upload(session_id): user_id = session.get_user_id(session_id) template_id = request.args.get("template_id") if template_id == None: return "Empty template", 500 user_templates = people.get_user_items("select * from (" + SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) if len(user_templates) == 0: return "Access denied", 500 user_template = user_templates[0] # file_path = os.path.abspath(os.path.dirname(__file__)) + '/templates/%s/template.py' % user_template["PATH"] file = request.files['file'] filename = secure_filename(file.filename) file.save( os.path.abspath(os.path.dirname(__file__)) + "/templates/" + user_template["PATH"] + "/files/" + filename) return "OK", 200
def delete_file(session_id): print "!" print request.data print "!!" user_id = session.get_user_id(session_id) template_id = request.args.get("template_id") params = json.loads(request.data) if template_id == None: return 500 user_templates = people.get_user_items( "select * from (" + consts.SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) if len(user_templates) == 0: return "Access denied", 500 user_template = user_templates[0] path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) + "/templates/" + user_template[ "PATH"] + "/files/" os.remove(path + params['file_name']) res = {} res["files"] = [f for f in os.listdir(path) if os.path.isfile(path + f)] return jsonify(res)
def get_file_list(session_id): user_id = session.get_user_id(session_id) template_id = request.args.get("template_id") if template_id == None: return 500 user_templates = people.get_user_items( "select * from (" + consts.SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) if len(user_templates) == 0: return "Access denied", 500 user_template = user_templates[0] template_path = user_template["PATH"] path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) + "/templates/%s" % template_path + "/files/" # path = u"%s" % path.decode('cp1251') # .encode('utf8').decode('utf8').replace(" ","_") # path = path.decode(sys.getfilesystemencoding()) #print path #tmp = os.listdir(path.decode("unicode_escape")) #print "2" files = [f for f in os.listdir(path) if os.path.isfile(path + f)] res = {} res["files"] = files return jsonify(res)
def generate_template(template_id, session_id): user_id = session.get_user_id(session_id) print user_id, template_id user_templates = people.get_user_items("select * from (" + SQL_GET_USER_TEMPLATES + ") where ID = :template_id", user_id, None, template_id=template_id) params = json.loads(request.data) template_request = parse_template_request(params) if len(user_templates) == 0: print "Access denied" return "Access denied", 500 user_template = user_templates[0] file_name = main.generate_picture(user_template["PATH"], template_request.width, template_request.height, template_request.format, float(template_request.dpi), user_id=user_id) return send_file(file_name), 200
def get_admin_templates(session_id, role): user_id = session.get_user_id(session_id) user_role = None if role == "user" else role res = {} res['templates'] = people.get_user_items(consts.SQL_GET_USER_TEMPLATES, user_id, user_role) return jsonify(res)