Guest User

sections.py

a guest
Jul 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.21 KB | None | 0 0
  1. from flask import request, jsonify
  2. from flask.views import MethodView
  3. from models import Section, Item, SubItem, db, ParameterValue
  4. from api.password import loadPassword
  5. from helper import Helper
  6. from sqlalchemy import or_
  7.  
  8. helper = Helper()
  9.  
  10.  
  11. class SectionsAPI(MethodView):
  12.  
  13.     def get(self, _id):
  14.         account_id = request.args.get('account')
  15.         if account_id is '': account_id = None
  16.         if _id:
  17.             if account_id:
  18.                 section = Section.query.filter(Section.id == _id).filter(or_(Section.account_id == None,
  19.                                                                              Section.account_id == account_id)).first()
  20.             else: section = Section.query.filter(Section.id == _id).filter(Section.account_id == None).first()
  21.  
  22.             if section: return jsonify(section.as_dict())
  23.             return '', 404
  24.         else: return jsonify({'sections': [sections.as_dict() for sections in Section.query.filter(
  25.                                             or_(Section.account_id == None, Section.account_id == account_id))]})
  26.  
  27.     @helper.require_json_keys('title', 'parameterValues', 'items')
  28.     def post(self):
  29.         username, password = None, None
  30.         if request.authorization:
  31.             username = request.authorization['username']
  32.             password = request.authorization['password']
  33.         data = request.json
  34.  
  35.         def create_items():
  36.             for i in data['items']:
  37.                 if 'description' in i and 'title' in i:
  38.                     item = Item(i['title'], i['description'], section.id)
  39.                     db.session.add(item)
  40.                     db.session.commit()
  41.                     for sub in i['subItems']:
  42.                         if 'title' in sub:
  43.                             sub_item = SubItem(sub['title'], item.id)
  44.                             db.session.add(sub_item)
  45.  
  46.         if 'accountId' in data and data['accountId']:
  47.             section = Section(data['title'], data['parameterValues'], data['accountId'])
  48.             db.session.add(section)
  49.             db.session.commit()
  50.  
  51.             create_items()
  52.             db.session.commit()
  53.             return jsonify(section.as_dict()), 201
  54.         else:
  55.             passwords = loadPassword()
  56.             if username in passwords and password == passwords[username]:
  57.                 section = Section(data['title'], data['parameterValues'])
  58.                 db.session.add(section)
  59.                 db.session.commit()
  60.  
  61.                 create_items()
  62.                 db.session.commit()
  63.                 return jsonify(section.as_dict()), 201
  64.             else: return '', 401
  65.  
  66.     @helper.require_json_keys()
  67.     def put(self, _id):
  68.         username, password = None, None
  69.         if request.authorization:
  70.             username = request.authorization['username']
  71.             password = request.authorization['password']
  72.         data = request.json
  73.  
  74.         def create_items():
  75.             for i in data['items']:
  76.                 if 'description' in i and 'title' in i:
  77.                     item = Item(i['title'], i['description'], section.id)
  78.                     db.session.add(item)
  79.                     db.session.commit()
  80.                     for sub in i['subItems']:
  81.                         if 'title' in sub:
  82.                             sub_item = SubItem(sub['title'], item.id)
  83.                             db.session.add(sub_item)
  84.  
  85.         section = Section.query.filter(Section.id == _id).filter(
  86.             or_(Section.id == _id, Section.account_id == None)).first()
  87.         if section:
  88.             passwords = loadPassword()
  89.             if section.account_id and not data['accountId'] and (not username or username not in passwords or
  90.                password != passwords[username]):
  91.                 return '', 401
  92.  
  93.             for it in section.items:
  94.                 db.session.delete(it)
  95.             db.session.commit()
  96.             create_items()
  97.  
  98.             if 'title' in data: section.title = data['title']
  99.             db.session.commit()
  100.  
  101.             if 'parameterValues' in data:
  102.                 parameter_values = ParameterValue.query.filter_by(section_id=section.id).all()
  103.                 for value in parameter_values:
  104.                     db.session.delete(value)
  105.                 db.session.commit()
  106.                 section.set_parameter_values(data['parameterValues'])
  107.  
  108.             return jsonify(section.as_dict())
  109.         else: return '', 404
  110.  
  111.     def delete(self, _id):
  112.         username, password = None, None
  113.         if request.authorization:
  114.             username = request.authorization['username']
  115.             password = request.authorization['password']
  116.         section = Section.query.filter_by(id=_id).first()
  117.         if section:
  118.             passwords = loadPassword()
  119.             if not section.account_id:
  120.                 if username and password and username in passwords and password == passwords[username]:
  121.                     db.session.delete(section)
  122.                     db.session.commit()
  123.                     return '', 204
  124.                 else: return '', 401
  125.             else:
  126.                 db.session.delete(section)
  127.                 db.session.commit()
  128.                 return '', 204
  129.         else: return '', 404
Add Comment
Please, Sign In to add comment