def changeName(): try: data = request.get_json() except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() if data and data['username']: user = User.getUser(username=data['username']) if user: if not verifyPassword(user, data['password']): return ErrorResponse(PasswordNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() user.name = data['name'] try: user.save_to_db() except Exception: return ErrorResponse(OperationNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() return jsonify( Response(200).generateMessage('Name Updated successfully')) else: return ErrorResponse(JsonNotFound().message, 422, { 'Content-Type': 'application/json' }).respond()
def register_sales(): schema = SalesSchema() input_data = request.get_json()['data']['attributes'] if 'email' in input_data.keys(): user = User.getUser(email=input_data['email']) if 'salesStat' in input_data.keys(): permission = Permissions.get_by_uid(user.id) permission.isSales = input_data['salesStat'] permission.save_to_db() user.save_to_db() return jsonify(schema.dump(user).data) else: return ErrorResponse(JsonNotFound().message, 422, { 'Content-Type': 'application/json' }).respond()
def admin_add_usage(): try: data = request.get_json()['data'] print(data) except Exception: return ErrorResponse(JsonNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data['uid'] allowed_usage = data['allowed_usage'] user = User.getUser(user_id=uid) user.allowed_usage = user.allowed_usage + allowed_usage db.session.commit() return jsonify(UserAllowedUsage().dump(user).data)
def reset_password(): try: data = request.get_json() except Exception: return ErrorResponse(PayloadNotFound().message, 422, {'Content-Type': 'application/json'}).respond() if data and data['username']: user = User.getUser(username=data['username']) expire = datetime.datetime.utcnow() + datetime.timedelta(hours=24) token = jwt.encode({ 'id': user.username, 'exp': expire }, app.config.get('SECRET_KEY')) return jsonify( Response(200).generateResetURL( token.decode('UTF-8'))) else: return ErrorResponse(JsonNotFound().message, 422, {'Content-Type': 'application/json'}).respond()
def update_user(userid): user = User.getUser(user_id=userid) permissions = Permissions.get_by_uid(userid) if not user: return ErrorResponse(UserNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() data = request.get_json()['data']['attributes'] if not data: return ErrorResponse(JsonNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() for key in data: if key in User.__table__.columns.keys(): setattr(user, key, data[key]) if key in Permissions.__table__.columns.keys(): setattr(permissions, key, data[key]) user.save_to_db() schema = AllUsersSchema() result = schema.dump(user) return jsonify(result.data)
def generateBadges(): try: data = request.get_json()['badge'] except Exception: return ErrorResponse(JsonNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() if not data['csv']: return ErrorResponse(CSVNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() if not data['image']: return ErrorResponse(ImageNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() csv_name = data.get('csv') csv_type = data.get('csv_type') or '' badge_name = data.get('badge_name') or 'My Badge' image_names = data.get('image').split(',') logo_image = data.get('logo_image') logo_text = data.get('logo_text') or '' logo_color = data.get('logo_color') or '#000000' font_color_1 = data.get('font_color_1') or '#ffffff' font_color_2 = data.get('font_color_2') or '#ffffff' font_color_3 = data.get('font_color_3') or '#ffffff' font_color_4 = data.get('font_color_4') or '#ffffff' font_color_5 = data.get('font_color_5') or '#ffffff' paper_size = data.get('paper_size') or 'A3' badge_size = data.get('badge_size') or '4x3' font_size_1 = data.get('font_size_1') or None font_size_2 = data.get('font_size_2') or None font_size_3 = data.get('font_size_3') or None font_size_4 = data.get('font_size_4') or None font_size_5 = data.get('font_size_5') or None font_type_1 = data.get('font_type_1') or 'helvetica' font_type_2 = data.get('font_type_2') or 'helvetica' font_type_3 = data.get('font_type_3') or 'helvetica' font_type_4 = data.get('font_type_4') or 'helvetica' font_type_5 = data.get('font_type_5') or 'helvetica' ticket_types = data.get('ticket_types').split(',') svg2png = SVG2PNG() if config.ENV == 'PROD': svg2png.do_text_fill( os.getcwd() + '/api/static/badges/8BadgesOnA3.svg', font_color_1, font_color_2, font_color_3, font_color_4, font_color_5, logo_color, badge_size, paper_size) svg2png.change_font_size( os.getcwd() + '/api/static/badges/8BadgesOnA3.svg', badge_size, paper_size, font_size_1, font_size_2, font_size_3, font_size_4, font_size_5) svg2png.change_font_family( os.getcwd() + '/api/static/badges/8BadgesOnA3.svg', badge_size, paper_size, font_type_1, font_type_2, font_type_3, font_type_4, font_type_5) else: svg2png.do_text_fill('static/badges/8BadgesOnA3.svg', logo_color, font_color_1, font_color_2, font_color_3, font_color_4, font_color_5, badge_size, paper_size) svg2png.change_font_size('static/badges/8BadgesOnA3.svg', badge_size, paper_size, font_size_1, font_size_2, font_size_3, font_size_4, font_size_5) svg2png.change_font_family( os.getcwd() + '/api/static/badges/8BadgesOnA3.svg', badge_size, paper_size, font_type_1, font_type_2, font_type_3, font_type_4, font_type_5) merge_badges = MergeBadges(image_names, logo_text, logo_image, csv_name, csv_type, paper_size, badge_size, ticket_types) merge_badges.merge_pdfs() uid = data.get('uid') user_creator = User.getUser(user_id=uid) if user_creator.allowed_usage == 0: return ErrorResponse(UsageNotAllowed().message, 403, { 'Content-Type': 'application/json' }).respond() user_creator.allowed_usage = user_creator.allowed_usage - 1 badge_created = Badges(csv=csv_name, font_color_1=font_color_1, font_color_2=font_color_2, font_color_3=font_color_3, font_color_4=font_color_4, font_color_5=font_color_5, font_size_1=font_size_1, font_size_2=font_size_2, font_size_3=font_size_3, font_size_4=font_size_4, font_size_5=font_size_5, font_type_1=font_type_1, font_type_2=font_type_2, font_type_3=font_type_3, font_type_4=font_type_4, font_type_5=font_type_5, paper_size=paper_size, logo_text=logo_text, logo_color=logo_color, logo_image=logo_image, image=",".join(image_names), badge_size=badge_size, badge_name=badge_name, creator=user_creator, csv_type=csv_type, ticket_types=",".join(ticket_types)) badge_created.save_to_db() badgeFolder = image_names[0].split('.')[0] badgePath = '' if config.ENV == 'LOCAL': badgePath = os.getcwd() + '/static/temporary/' + badgeFolder else: badgePath = os.getcwd() + '/api/static/temporary/' + badgeFolder if os.path.isdir(badgePath): image_links = [] for image in image_names: imageDirectory = os.path.join(badgePath, '../../uploads/image', image) link = fileUploader(imageDirectory, 'images/' + image) image_links.append(link) badge_created.image_link = ",".join(image_links) if logo_image != '': logoImageDirectory = os.path.join(badgePath, '../../uploads/image', logo_image) link = fileUploader(logoImageDirectory, 'images/' + logo_image) badge_created.logo_image_link = link else: badge_created.logo_image_link = '' link = fileUploader(badgePath + '/all-badges.pdf', 'badges/' + badge_created.id + '.pdf') send_badge_mail(badge_created.id, user_creator.id, link) badge_created.download_link = link rmtree(badgePath, ignore_errors=True) db.session.commit() return jsonify(BadgeSchema().dump(badge_created).data)
def editBadges(badgeId): try: data = request.get_json()['badge'] except Exception: return ErrorResponse(JsonNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() try: badge = Badges.getBadge(badgeId) except Exception: return ErrorResponse(JsonNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data.get('uid') user_creator = User.getUser(user_id=uid) del data['uid'] badge.update(data) svg2png = SVG2PNG() _badge = badge.first() badgeFolder = _badge.image.split(',')[0].split('.')[0] badgePath = '' if config.ENV == 'LOCAL': badgePath = os.getcwd() + '/static/temporary/' + badgeFolder else: badgePath = os.getcwd() + '/api/static/temporary/' + badgeFolder rmtree(badgePath, ignore_errors=True) if config.ENV == 'PROD': svg2png.do_text_fill( os.getcwd() + '/api/static/badges/8BadgesOnA3.svg', _badge.font_color_1, _badge.font_color_2, _badge.font_color_3, _badge.font_color_4, _badge.font_color_5, _badge.logo_color, _badge.badge_size, _badge.paper_size) svg2png.change_font_size( os.getcwd() + '/api/static/badges/8BadgesOnA3.svg', _badge.badge_size, _badge.paper_size, _badge.font_size_1, _badge.font_size_2, _badge.font_size_3, _badge.font_size_4, _badge.font_size_5) svg2png.change_font_family( os.getcwd() + '/api/static/badges/8BadgesOnA3.svg', _badge.badge_size, _badge.paper_size, _badge.font_type_1, _badge.font_type_2, _badge.font_type_3, _badge.font_type_4, _badge.font_type_5) else: svg2png.do_text_fill('static/badges/8BadgesOnA3.svg', _badge.logo_color, _badge.font_color_1, _badge.font_color_2, _badge.font_color_3, _badge.font_color_4, _badge.font_color_5, _badge.badge_size, _badge.paper_size) svg2png.change_font_size('static/badges/8BadgesOnA3.svg', _badge.badge_size, _badge.paper_size, _badge.font_size_1, _badge.font_size_2, _badge.font_size_3, _badge.font_size_4, _badge.font_size_5) svg2png.change_font_family( os.getcwd() + '/api/static/badges/8BadgesOnA3.svg', _badge.badge_size, _badge.paper_size, _badge.font_type_1, _badge.font_type_2, _badge.font_type_3, _badge.font_type_4, _badge.font_type_5) merge_badges = MergeBadges(_badge.image.split(','), _badge.logo_text, _badge.logo_image, _badge.csv, _badge.csv_type, _badge.paper_size, _badge.badge_size, _badge.ticket_types.split(',')) merge_badges.merge_pdfs() if os.path.isdir(badgePath): image_links = [] for image in _badge.image.split(','): imageDirectory = os.path.join(badgePath, '../../uploads/image', image) link = fileUploader(imageDirectory, 'images/' + image) image_links.append(link) _badge.image_link = ",".join(image_links) if _badge.logo_image != '': logoImageDirectory = os.path.join(badgePath, '../../uploads/image', _badge.logo_image) link = fileUploader(logoImageDirectory, 'images/' + _badge.logo_image) _badge.logo_image_link = link else: _badge.logo_image_link = '' link = fileUploader(badgePath + '/all-badges.pdf', 'badges/' + str(uuid.uuid4()) + '.pdf') send_badge_mail(_badge.id, user_creator.id, link) _badge.download_link = link rmtree(badgePath, ignore_errors=True) db.session.commit() return jsonify(BadgeSchema().dump(_badge).data)