Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime
- from api.api import db
- from werkzeug.security import generate_password_hash, check_password_hash
- from flask import request, jsonify
- from api.setting import ConfigServise
- class User(db.Model):
- __tablename__ = 'users'
- id = db.Column(db.Integer, primary_key=True)
- email = db.Column(db.String(80), primary_key=True, unique=True)
- login = db.Column(db.String(124))
- name = db.Column(db.String(80), default="User")
- sername = db.Column(db.String(80), default="User")
- patronymic = db.Column(db.String(80), default="User")
- password_hash = db.Column(db.String(128))
- datetime_joined = db.Column(db.DateTime, default=datetime.now())
- photo = db.Column(db.String(255))
- def __init__(self, *args, password, **kwargs):
- super(User, self).__init__(*args, **kwargs)
- self.set_password(password)
- def __repr__(self):
- return '<User %r>' % self.email
- def set_password(self, password):
- self.password_hash = generate_password_hash(str(password))
- def check_password(self, password):
- return check_password_hash(self.password_hash, password)
- def get_id(self):
- return str(self.email)
- def edit_profile(self, request):
- """Edit profile, parameters are passed from request"""
- try:
- name = request.form['name']
- file = request.files.get('photo', '')
- if name:
- self.name = name
- if file:
- self.upload_file(request)
- self.photo = file.filename
- db.session.commit()
- response = jsonify({"message" : f"Profile fields: name = {name}, file = {file} Parametrs successfully update"})
- response.status_code = 200
- return response
- except Exception as e:
- print(e)
- response = jsonify({"message" : "Server internal"})
- response.status_code = 500
- return response
- def upload_file(self, request: request):
- if 'files' not in request.files:
- response = jsonify(
- {
- "messege" : "No file part in the request"
- }
- )
- response.status_code = 400
- return response
- file = request.files.get('photo', '')
- errors = {}
- success = False
- print(self.allowed_file(file.filename))
- if file and self.allowed_file(file.filename):
- filename = secure_filename(file.filename)
- path = os.path.join(ConfigServise.UPLOAD_FOLDER, filename)
- file.save(path)
- success = True
- else:
- errors[file.filename] = 'File type is not allowed'
- if success and errors:
- errors['message'] = 'File(s) successfully uploaded'
- response = jsonify(errors)
- response.status_code = 500
- return response
- if success:
- response = jsonify({'message' : 'Files successfully uploaded'})
- response.status_code = 201
- return response
- else:
- response = jsonify(errors)
- response.status_code = 500
- return response
- def allowed_file(self, filename):
- return '.' in filename and filename.rsplit('.', 1)[1].lower() in ConfigServise.ALLOWED_EXTENSIONS
- def __str__(self):
- return str(self.id) + ':' +str(self.name)
- product_price = db.Table(
- 'product_price',
- db.Column('product_id', db.Integer(), db.ForeignKey('product.id')),
- db.Column('price_id', db.Integer(), db.ForeignKey('price.id'))
- )
- class Product(db.Model):
- __tablename__ = 'product'
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String(128))
- photo = db.Column(db.String(255))
- vendor_code = db.Column(db.String(12))
- price = db.relationship('Price', secondary=product_price, backref=db.backref('price', lazy='dynamic'))
- def upload_file(self, request: request):
- if 'files' not in request.files:
- response = jsonify(
- {
- "messege" : "No file part in the request"
- }
- )
- response.status_code = 400
- return response
- file = request.files.get('photo', '')
- errors = {}
- success = False
- if file and self.allowed_file(file.filename):
- filename = secure_filename(file.filename)
- path = os.path.join(ConfigServise.UPLOAD_FOLDER, filename)
- file.save(path)
- success = True
- else:
- errors[file.filename] = 'File type is not allowed'
- if success and errors:
- errors['message'] = 'File(s) successfully uploaded'
- response = jsonify(errors)
- response.status_code = 500
- return response
- if success:
- response = jsonify({'message' : 'Files successfully uploaded'})
- response.status_code = 201
- return response
- else:
- response = jsonify(errors)
- response.status_code = 500
- return response
- def allowed_file(self, filename):
- return '.' in filename and filename.rsplit('.', 1)[1].lower() in ConfigServise.ALLOWED_EXTENSIONS
- class Price(db.Model):
- __tablename__ = 'price'
- id = db.Column(db.Integer, primary_key=True)
- def init_db():
- db.create_all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement