Advertisement
Mochinov

Untitled

Aug 31st, 2021
1,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.49 KB | None | 0 0
  1. from datetime import datetime
  2. from api.api import db
  3. from werkzeug.security import generate_password_hash, check_password_hash
  4.  
  5. from flask import request, jsonify
  6. from api.setting import ConfigServise
  7.  
  8. class User(db.Model):
  9.     __tablename__ = 'users'
  10.  
  11.     id = db.Column(db.Integer, primary_key=True)
  12.     email = db.Column(db.String(80), primary_key=True, unique=True)
  13.     login = db.Column(db.String(124))
  14.     name = db.Column(db.String(80), default="User")
  15.     sername = db.Column(db.String(80), default="User")
  16.     patronymic = db.Column(db.String(80), default="User")
  17.     password_hash = db.Column(db.String(128))
  18.     datetime_joined = db.Column(db.DateTime, default=datetime.now())
  19.     photo = db.Column(db.String(255))
  20.  
  21.  
  22.     def __init__(self, *args, password,  **kwargs):
  23.         super(User, self).__init__(*args, **kwargs)
  24.         self.set_password(password)
  25.  
  26.     def __repr__(self):
  27.         return '<User %r>' % self.email
  28.  
  29.     def set_password(self, password):
  30.         self.password_hash = generate_password_hash(str(password))
  31.  
  32.     def check_password(self, password):
  33.         return check_password_hash(self.password_hash, password)
  34.  
  35.     def get_id(self):
  36.         return str(self.email)
  37.  
  38.     def edit_profile(self, request):
  39.         """Edit profile, parameters are passed from request"""
  40.         try:
  41.             name = request.form['name']
  42.             file = request.files.get('photo', '')
  43.  
  44.             if name:
  45.                 self.name = name
  46.             if file:
  47.                 self.upload_file(request)
  48.                 self.photo = file.filename
  49.  
  50.             db.session.commit()
  51.             response = jsonify({"message" : f"Profile fields: name = {name}, file = {file} Parametrs successfully update"})
  52.             response.status_code = 200
  53.             return response
  54.         except Exception as e:
  55.             print(e)
  56.             response = jsonify({"message" : "Server internal"})
  57.             response.status_code = 500
  58.             return response
  59.          
  60.  
  61.  
  62.  
  63.     def upload_file(self, request: request):
  64.         if 'files' not in request.files:
  65.             response = jsonify(
  66.                 {
  67.                     "messege" : "No file part in the request"
  68.                 }
  69.             )
  70.             response.status_code = 400
  71.             return response
  72.         file = request.files.get('photo', '')
  73.         errors = {}
  74.         success = False
  75.         print(self.allowed_file(file.filename))
  76.         if file and self.allowed_file(file.filename):
  77.             filename = secure_filename(file.filename)
  78.             path = os.path.join(ConfigServise.UPLOAD_FOLDER, filename)
  79.             file.save(path)
  80.             success = True
  81.         else:
  82.             errors[file.filename] = 'File type is not allowed'
  83.    
  84.         if success and errors:
  85.             errors['message'] = 'File(s) successfully uploaded'
  86.             response = jsonify(errors)
  87.             response.status_code = 500
  88.             return response
  89.         if success:
  90.             response = jsonify({'message' : 'Files successfully uploaded'})
  91.             response.status_code = 201
  92.             return response
  93.  
  94.         else:
  95.             response = jsonify(errors)
  96.             response.status_code = 500
  97.             return response
  98.    
  99.     def allowed_file(self, filename):
  100.         return '.' in filename and filename.rsplit('.', 1)[1].lower() in ConfigServise.ALLOWED_EXTENSIONS
  101.  
  102.     def __str__(self):
  103.         return str(self.id) + ':' +str(self.name)
  104.  
  105.  
  106.  
  107.  
  108. product_price = db.Table(
  109.     'product_price',
  110.     db.Column('product_id', db.Integer(), db.ForeignKey('product.id')),
  111.     db.Column('price_id', db.Integer(), db.ForeignKey('price.id'))
  112. )
  113.  
  114. class Product(db.Model):
  115.     __tablename__ = 'product'
  116.  
  117.     id = db.Column(db.Integer, primary_key=True)
  118.     name = db.Column(db.String(128))
  119.     photo = db.Column(db.String(255))
  120.     vendor_code = db.Column(db.String(12))
  121.     price = db.relationship('Price', secondary=product_price, backref=db.backref('price', lazy='dynamic'))
  122.  
  123.  
  124.  
  125.     def upload_file(self, request: request):
  126.         if 'files' not in request.files:
  127.             response = jsonify(
  128.                 {
  129.                     "messege" : "No file part in the request"
  130.                 }
  131.             )
  132.             response.status_code = 400
  133.             return response
  134.         file = request.files.get('photo', '')
  135.         errors = {}
  136.         success = False
  137.         if file and self.allowed_file(file.filename):
  138.             filename = secure_filename(file.filename)
  139.             path = os.path.join(ConfigServise.UPLOAD_FOLDER, filename)
  140.             file.save(path)
  141.             success = True
  142.         else:
  143.             errors[file.filename] = 'File type is not allowed'
  144.    
  145.         if success and errors:
  146.             errors['message'] = 'File(s) successfully uploaded'
  147.             response = jsonify(errors)
  148.             response.status_code = 500
  149.             return response
  150.         if success:
  151.             response = jsonify({'message' : 'Files successfully uploaded'})
  152.             response.status_code = 201
  153.             return response
  154.  
  155.         else:
  156.             response = jsonify(errors)
  157.             response.status_code = 500
  158.             return response
  159.    
  160.     def allowed_file(self, filename):
  161.         return '.' in filename and filename.rsplit('.', 1)[1].lower() in ConfigServise.ALLOWED_EXTENSIONS
  162.  
  163.  
  164. class Price(db.Model):
  165.     __tablename__ = 'price'
  166.     id = db.Column(db.Integer, primary_key=True)
  167.  
  168.  
  169.  
  170.  
  171. def init_db():
  172.     db.create_all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement