def edit_news_category_post(): v = Validator(request.form) v.field("name_category").required() if v.is_valid(): name_category = v.valid_data.name_category NewsCategory.rename(request.form.get("category_id"), name_category) return jsonify({"status": "ok"}) return jsonify({"status": "fail", "errors": v.errors})
def add_structure_post(): v = Validator(request.form) v.field("name_structure").required() if v.is_valid(): name_structure = v.valid_data.name_structure Department.add(request.form.get("department_id"), name_structure) return jsonify({"status": "ok"}) return jsonify({"status": "fail", "errors": v.errors})
def edit_news_category_post(): v = Validator(request.form) v.field("name_category").required() if v.is_valid(): name_category = v.valid_data.name_category NewsCategory.rename(request.form.get("category_id"), name_category) return jsonify({"status": "ok"}) return jsonify({"status": "fail", "errors": v.errors})
def restore_post(): v = Validator(request.form) v.field('email').required().email() if v.is_valid(): email = request.form.get("email") user = User.get_by_email(email) if user: token = PasswordRestore.add_token(user) print(token) send_password_restore_ref.delay(user.email, user.full_name, token) return jsonify({"status": "ok"}) return jsonify({"status": "fail", "errors": v.errors})
def like_route(): user = auth.service.get_user() v = Validator(request.form) v.field("value").boolean().required() v.field("entity").required() v.field("entity_id").integer().required() if user.is_authorized() and v.is_valid(): data = v.valid_data vote = Vote.get_for(data.entity, data.entity_id, user) or Vote() vote.user = user vote.entity = data.entity vote.entity_id = data.entity_id vote.value = 0 if vote.value else 1 vote.type = Vote.Type.LIKE db.session.add(vote) delta = 1 if vote.value else -1 if vote.id else 0 entity = vote.get_entity() if entity: entity.after_update_vote(delta) db.session.commit() return jsonify({"status": "ok", "vote": vote.as_dict(), "count": entity.votes_count if entity else 0}) return jsonify({"status": "fail"})
def like_route(): user = auth.service.get_user() v = Validator(request.form) v.field('value').boolean().required() v.field('entity').required() v.field('entity_id').integer().required() if user.is_authorized() and v.is_valid(): data = v.valid_data vote = Vote.get_for(data.entity, data.entity_id, user) or Vote() vote.user = user vote.entity = data.entity vote.entity_id = data.entity_id vote.value = 0 if vote.value else 1 vote.type = Vote.Type.LIKE db.session.add(vote) delta = 1 if vote.value else -1 if vote.id else 0 entity = vote.get_entity() if entity: entity.after_update_vote(delta) db.session.commit() return jsonify({'status': 'ok', 'vote': vote.as_dict(), 'count': entity.votes_count if entity else 0}) return jsonify({'status': 'fail'}) # @module.post("/vote/<int:value>") # def vote(value, type): # user = auth.service.get_user() # v = Validator(request.form) # v.field('entity').required() # # if user.is_authorized() and v.is_valid(): # data = v.valid_data # vote = Vote.get_for(entity=data.entity, user=user) or Vote() # vote.user = user # vote.entity = data.entity # vote.value = value # vote.type = type # db.session.add() # # entity = vote.get_entity() # # if entity: # entity.after_add_vote(vote) # # db.session.commit() # return jsonify({'status': 'ok', # 'vote': vote.as_dict()}) # # return jsonify({'status': 'fail'})
def login_post(): v = Validator(request.form) v.field("login").required() v.field("password").required() if v.is_valid(): login = v.valid_data.login password = v.valid_data.password if auth.service.login(login, password): return jsonify({"status": "ok"}) else: v.add_error('login', 'Логин или пароль не верен', 'wrong_login_or_password') return jsonify({"status": "fail", "errors": v.errors})
def upload(ftype=None): v = Validator({'file': request.files['file']}) if ftype == 'image': v.field('file').image() if v.is_valid(): file = v.valid_data.file ext = file.filename.split('.')[-1] uid = str(uuid.uuid4()).replace('-', '') directory = os.path.join(application.files_folder, 'uploads') if not os.path.exists(directory): os.makedirs(directory) path = os.path.join(directory, 'file.%s.%s' % (uid, ext)) v.valid_data.file.save(path) return jsonify({'status': 'ok', 'file': { 'url': '/file/uploads/%s' % os.path.basename(path) }}) return jsonify({'status': 'fail', 'errors': v.errors})
def upload(ftype=None): v = Validator({'file': request.files['file']}) if ftype == 'image': v.field('file').image() if v.is_valid(): file = v.valid_data.file ext = file.filename.split('.')[-1] uid = str(uuid.uuid4()).replace('-', '') directory = os.path.join(application.files_folder, 'uploads') if not os.path.exists(directory): os.makedirs(directory) path = os.path.join(directory, 'file.%s.%s' % (uid, ext)) v.valid_data.file.save(path) return jsonify({ 'status': 'ok', 'file': { 'url': '/file/uploads/%s' % os.path.basename(path) } }) return jsonify({'status': 'fail', 'errors': v.errors})
def new_pass_post(): v = Validator(request.form) v.field('password_1').required() v.field('password_2').required() v.field('password_2').equal(v.field('password_1'), message="Повторый пароль неверный") if v.is_valid(): restore_pass = PasswordRestore.is_valid_token(request.form.get("token")) if not restore_pass: abort(404) new_password = request.form.get("password_1") restore_password(restore_pass.author.login, new_password) PasswordRestore.deactivation_token(restore_pass) return jsonify({"status": "ok"}) return jsonify({"status": "fail", "errors": v.errors})
def edit_profile(): current_user = auth.service.get_user() _data = dict(request.form) _data["file"] = request.files["file"] v = Validator(_data) v.field('full_name').required() v.field('email').required().email() v.field('mobile_phone').required().phone_number() v.field('department').required() v.field('birth_date').datetime(format="%d.%m.%Y") v.field('file').image() if v.is_valid(): data = { 'id': current_user.id, 'full_name': v.valid_data.full_name, 'position': v.valid_data.position, 'mobile_phone': v.valid_data.mobile_phone, 'inner_phone': v.valid_data.inner_phone, 'department': v.valid_data.department, 'email': v.valid_data.email, 'skype': v.valid_data.skype, 'photo': v.valid_data.file, 'birth_date': v.valid_data.birth_date } try: update_user(**data) return {"status": "ok"} except DataProcessingError as e: return {'status': 'failOnProcess', 'error': e.value} return {"status": "fail", "errors": v.errors}
def save_quote(id=None): user = auth.service.get_user() data = dict(request.form) data['upload'] = request.files.getlist('upload') v = Validator(data) v.fields('upload').image() v.fields('file.id').integer(nullable=True) if v.is_valid(): if not id: v.field('quote_for').integer().required() v.field('entity_name').required() v.field('entity_id').integer(nullable=True).required() if not v.valid_data.list('url') and not v.valid_data.list( 'upload'): v.field('comment').required( message="Напишите хоть что-нибудь...") if v.is_valid() and user.is_authorized(): data = v.valid_data comment = None if not id: quote_for = Comment.get(v.valid_data.quote_for) if quote_for: comment = Comment() comment.author_id = user.id comment.quote_for = quote_for comment.entity = quote_for.entity comment.entity_id = quote_for.entity_id else: comment = Comment.get(id) if comment: comment.modify_datetime = datetime.datetime.now() comment.status = Comment.Status.MODIFIED if comment: return save(comment, data) v.add_error('comment', 'Что-то пошло не так... Попробуйте позже.') return jsonify({'status': 'fail', 'errors': v.errors})
def validate_bar_registering(): validator = Validator(request) validator.validate(request.form['address'] is None, "Campo vazio") validator.validate_image(request.files['image'].filename) validator.on_error_send_bad_request()
def add_user(): _data = dict(request.form) _data['file'] = request.files['file'] v = Validator(_data) v.field('name').required() v.field('surname').required() v.field('file').image() v.field('email').required().email() v.field('login').required() v.field('department').required() v.field('groups').required() v.field('mobile_phone').required().phone_number() v.field('birth_date').datetime(format='%d.%m.%Y') if v.is_valid(): data = { 'name': v.valid_data.name, 'surname': v.valid_data.surname, 'email': v.valid_data.email, 'login': v.valid_data.login, 'department': v.valid_data.department, 'groups': v.valid_data.list('groups'), 'mobile_phone': v.valid_data.mobile_phone, 'skype': v.valid_data.skype, 'birth_date': v.valid_data.birth_date, 'photo': v.valid_data.file } already_used_login = User.get_by_login(data['login']) already_used_email = User.get_by_email(data['email']) if already_used_login: v.add_error('login', 'Такой логин уже занят') if already_used_email: v.add_error('email', 'Такой email уже занят') if already_used_login or already_used_email: return {"status": "fail", "errors": v.errors} try: create_user(**data) return {"status": "ok"} except DataProcessingError as e: return {'status': 'failOnProcess', 'error': e.value} return {"status": "fail", "errors": v.errors}
def save(): data = dict(request.form) data['image'] = request.files.getlist('image') v = Validator(data) v.fields('id').integer(nullable=True) v.field('name').required() v.field('private').boolean() v.field('description').required() v.field('image').image() user = auth.service.get_user() if not user.is_authorized(): abort(403) if not v.is_valid(): return jsonify({ 'status': 'fail', 'errors': v.errors }) data = v.valid_data community = Community.get(data.id) if community: community.type = Community.TYPE.PRIVATE if data.private else Community.TYPE.PUBLIC community.name = data.name community.description = data.description else: community = Community() community.type = Community.TYPE.PRIVATE if data.private else Community.TYPE.PUBLIC community.name = data.name community.description = data.description community.owner = user db.session.add(community) db.session.flush() image = data.image if image: img = community.image = community.image or File.create(name='image.png', module='community', entity=community) img.makedir() img.update_hash() utils.image.thumbnail(image, width=200, height=200, fill=utils.image.COVER).save(img.get_path()) db.session.commit() return jsonify({'status': 'ok', 'community': community.as_dict()})
def post_save(): v = Validator(request.form) v.fields('id').integer(nullable=True) v.field('title').required() v.field('text').required() v.field('community_id').required().integer() user = auth.service.get_user() if not user.is_authorized(): abort(403) if not v.is_valid(): return jsonify({ 'status': 'fail', 'errors': v.errors }) data = v.valid_data if not data.id: post = Post() post.community_id = data.community_id else: post = Post.get(data.id) if not post: abort(400) if post.author and post.author != user: abort(403) post.title = data.title post.text = data.text post.author = user db.session.add(post) db.session.commit() return jsonify({ 'status': 'ok', 'post': post.as_dict() })
def add_user(): _data = dict(request.form) _data['file'] = request.files['file'] v = Validator(_data) v.field('name').required() v.field('surname').required() v.field('file').image() v.field('email').required().email() v.field('login').required() v.field('department').required() v.field('groups').required() v.field('mobile_phone').required().phone_number() v.field('birth_date').datetime(format='%d.%m.%Y') if v.is_valid(): data = { 'name': v.valid_data.name, 'surname': v.valid_data.surname, 'email': v.valid_data.email, 'login': v.valid_data.login, 'department': v.valid_data.department, 'groups': v.valid_data.list('groups'), 'mobile_phone': v.valid_data.mobile_phone, 'skype': v.valid_data.skype, 'birth_date': v.valid_data.birth_date, 'photo': v.valid_data.file } already_used_login = User.get_by_login(data['login']) already_used_email = User.get_by_email(data['email']) if already_used_login: v.add_error('login', 'Такой логин уже занят') if already_used_email: v.add_error('email', 'Такой email уже занят') if already_used_login or already_used_email: return {"status": "fail", "errors": v.errors} try: create_user(**data) return {"status": "ok"} except DataProcessingError as e: return {'status': 'failOnProcess', 'error': e.value} return {"status": "fail", "errors": v.errors}
def save_quote(id=None): user = auth.service.get_user() data = dict(request.form) data['upload'] = request.files.getlist('upload') v = Validator(data) v.fields('upload').image() v.fields('file.id').integer(nullable=True) if v.is_valid(): if not id: v.field('quote_for').integer().required() v.field('entity_name').required() v.field('entity_id').integer(nullable=True).required() if not v.valid_data.list('url') and not v.valid_data.list('upload'): v.field('comment').required(message="Напишите хоть что-нибудь...") if v.is_valid() and user.is_authorized(): data = v.valid_data comment = None if not id: quote_for = Comment.get(v.valid_data.quote_for) if quote_for: comment = Comment() comment.author_id = user.id comment.quote_for = quote_for comment.entity = quote_for.entity comment.entity_id = quote_for.entity_id else: comment = Comment.get(id) if comment: comment.modify_datetime = datetime.datetime.now() comment.status = Comment.Status.MODIFIED if comment: return save(comment, data) v.add_error('comment', 'Что-то пошло не так... Попробуйте позже.') return jsonify({'status': 'fail', 'errors': v.errors})
def edit_user(id): _data = dict(request.form) _data['file'] = request.files['file'] v = Validator(_data) v.field('name').required() v.field('surname').required() v.field('file').image() v.field('email').required().email() v.field('department').required() v.field('inner_phone').integer(nullable=True) v.field('mobile_phone').required().phone_number() v.field('birth_date').datetime(format='%d.%m.%Y') if v.is_valid(): edited_user = User.get_by_id(id) duplicate_error = False data = { 'id': id, 'full_name': "{0} {1}".format(v.valid_data.name, v.valid_data.surname), 'email': v.valid_data.email, 'department': v.valid_data.department, 'mobile_phone': v.valid_data.mobile_phone, 'inner_phone': v.valid_data.inner_phone, 'position': v.valid_data.position, 'skype': v.valid_data.skype, 'birth_date': v.valid_data.birth_date, 'photo': v.valid_data.file } user_with_same_email = User.get_by_email(data['email']) if user_with_same_email and user_with_same_email != edited_user: duplicate_error = True v.add_error('email', 'Такой email уже занят') if duplicate_error: return {"status": "fail", "errors": v.errors} try: update_user(**data) return {"status": "ok"} except DataProcessingError as e: return {'status': 'failOnProcess', 'error': e.value} return {"status": "fail", "errors": v.errors}
def edit_profile(): current_user = auth.service.get_user() _data = dict(request.form) _data["file"] = request.files["file"] v = Validator(_data) v.field('full_name').required() v.field('email').required().email() v.field('mobile_phone').required().phone_number() v.field('department').required() v.field('birth_date').datetime(format="%d.%m.%Y") v.field('file').image() if v.is_valid(): data = { 'id': current_user.id, 'full_name': v.valid_data.full_name, 'position': v.valid_data.position, 'mobile_phone': v.valid_data.mobile_phone, 'inner_phone': v.valid_data.inner_phone, 'department': v.valid_data.department, 'email': v.valid_data.email, 'skype': v.valid_data.skype, 'photo': v.valid_data.file, 'birth_date': v.valid_data.birth_date } try: update_user(**data) return {"status": "ok"} except DataProcessingError as e: return {'status': 'failOnProcess', 'error': e.value} return {"status": "fail", "errors": v.errors}
def edit_pass_post(): current_user = auth.service.get_user() v = Validator(request.form) v.field('password_old').required() v.field('password_1').required() v.field('password_2').required() v.field('password_2').equal(v.field('password_1'), message="Повторный пароль неверный") if v.is_valid(): old_password = request.form.get("password_old") new_password = request.form.get("password_1") try: modify_password(current_user.login, old_password, new_password) except PasswordError: v.add_error('password_old', 'Неверный пароль') return jsonify({"status": "fail", "errors": v.errors}) return jsonify({"status": "ok"}) return jsonify({"status": "fail", "errors": v.errors})
def test(): v = Validator(request.form) v.fields('email').email() v.field('first_name').integer() v.field('text').required().length(max=6) v.field('image').image() if v.is_valid(): return jsonify({'status': 'ok'}) return jsonify({'status': 'fail', 'errors': v.errors}) # @main.route('/parse_asl') # def parser_asf(): # with open('application/static/images/smiles/tango/MSNTango.asl', 'r') as f: # for line in f.readlines(): # parts = line.split(' "') # if len(parts) > 2: # file = parts[1].split('"')[0].strip('",') # text = parts[2].strip('",').strip('-125, ').split() # if len(file) > 1: # print('"/static/images/smiles/tango/'+file+'":', '["'+'", "'.join(text)+'"],')
def edit_user(id): _data = dict(request.form) _data['file'] = request.files['file'] v = Validator(_data) v.field('name').required() v.field('surname').required() v.field('file').image() v.field('email').required().email() v.field('department').required() v.field('inner_phone').integer(nullable=True) v.field('mobile_phone').required().phone_number() v.field('birth_date').datetime(format='%d.%m.%Y') if v.is_valid(): edited_user = User.get_by_id(id) duplicate_error = False data = { 'id': id, 'full_name': "{0} {1}".format(v.valid_data.name, v.valid_data.surname), 'email': v.valid_data.email, 'department': v.valid_data.department, 'mobile_phone': v.valid_data.mobile_phone, 'inner_phone': v.valid_data.inner_phone, 'position': v.valid_data.position, 'skype': v.valid_data.skype, 'birth_date': v.valid_data.birth_date, 'photo': v.valid_data.file } user_with_same_email = User.get_by_email(data['email']) if user_with_same_email and user_with_same_email != edited_user: duplicate_error = True v.add_error('email', 'Такой email уже занят') if duplicate_error: return {"status": "fail", "errors": v.errors} try: update_user(**data) return {"status": "ok"} except DataProcessingError as e: return {'status': 'failOnProcess', 'error': e.value} return {"status": "fail", "errors": v.errors}
def save(): v = Validator(request.form) v.fields('id').integer(nullable=True) v.field('title').required() v.field('text').required() v.field('category_id').integer(nullable=True) user = auth.service.get_user() if not user.is_authorized(): abort(403) if v.is_valid(): data = v.valid_data news = News.get(data.id) if news: news.title = data.title news.text = data.text else: news = News() news.title = data.title news.text = data.text news.author = user category = NewsCategory.get(data.category_id) news.category = category tags = data.list('tag') existing_tags = {tag.name: tag for tag in NewsTag.get_tags(tags)} tags = {tag: NewsTag(name=tag) for tag in tags} tags.update(existing_tags) news.tags = list(tags.values()) is_new = True if user.id is not None else False db.session.add(news) db.session.commit() if is_new: send_news_notification.delay(news.id, news.title) return jsonify({'status': 'ok', 'news': news.as_dict()}) return jsonify({'status': 'fail', 'errors': v.errors})
def edit_pass_post(): current_user = auth.service.get_user() v = Validator(request.form) v.field('password_old').required() v.field('password_1').required() v.field('password_2').required() v.field('password_2').equal(v.field('password_1'), message="Повторный пароль неверный") if v.is_valid(): old_password = request.form.get("password_old") new_password = request.form.get("password_1") try: modify_password(current_user.login, old_password, new_password) except PasswordError: v.add_error('password_old', 'Неверный пароль') return jsonify({"status": "fail", "errors": v.errors}) return jsonify({"status": "ok"}) return jsonify({"status": "fail", "errors": v.errors})