Advertisement
TwiNNeR

eve

Jul 5th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """Borg model"""
  3. from serverapi.database import Column, Model, SurrogatePK, db
  4. from marshmallow_jsonapi import Schema, fields
  5. from marshmallow import validate
  6.  
  7. not_blank = validate.Length(min=1, error='Field cannot be blank')
  8.  
  9.  
  10. def get_info_model():
  11.     if Borg.query.count() == 0:
  12.         info_model = Borg()
  13.         info_model.save()
  14.     else:
  15.         info_model = Borg.query.first()
  16.  
  17.     return info_model
  18.  
  19.  
  20. class Borg(SurrogatePK, Model):
  21.     """Borg model"""
  22.  
  23.     __tablename__ = 'borg'
  24.  
  25.     def __init__(self, **kwargs):
  26.         """Create instance"""
  27.         db.Model.__init__(self, **kwargs)
  28.  
  29.  
  30. class BorgSchema(Schema):
  31.     id = fields.Integer(dump_only=True)
  32.  
  33.     # self links
  34.     def get_top_level_links(self, data, many):
  35.         if many:
  36.             self_link = ""
  37.         else:
  38.             self_link = "/borg"
  39.         return {'self': self_link}
  40.  
  41.     class Meta:
  42.         type_ = 'borg'
  43.         strict = True
  44.  
  45.  
  46. class BorgProcess(SurrogatePK, Model):
  47.     """Borg Process Model"""
  48.  
  49.     __tablename__ = 'borg_process'
  50.  
  51.     name = Column(db.String(500))
  52.     type = Column(db.String(500))
  53.     source = Column(db.String(500))
  54.     repo_name = Column(db.String(500))
  55.     samba_user = Column(db.String(100))
  56.     destination_host = Column(db.String(500))
  57.     destination_user = Column(db.String(100))
  58.     destination_password = Column(db.String(100))
  59.  
  60.     def __init__(self, request_dict, **kwargs):
  61.         """Create instance"""
  62.         self.name = request_dict['name']
  63.         self.type = request_dict['type']
  64.         self.source = request_dict['source']
  65.         self.repo_name = request_dict['repo_name']
  66.         self.samba_user = request_dict['samba_user']
  67.         self.destination_host = request_dict['destination_host']
  68.         self.destination_user = request_dict['destination_user']
  69.         self.destination_password = request_dict['destination_password']
  70.  
  71.         db.Model.__init__(self, **kwargs)
  72.  
  73.  
  74. class BorgProcessSchema(Schema):
  75.     id = fields.Integer(dump_only=True)
  76.     name = fields.String(validate=not_blank)
  77.     type = fields.String(validate=not_blank)
  78.     source = fields.String(validate=not_blank)
  79.     repo_name = fields.String(validate=not_blank)
  80.     samba_user = fields.String(validate=not_blank)
  81.     destination_host = fields.String(validate=not_blank)
  82.     destination_user = fields.String(validate=not_blank)
  83.     destination_password = fields.String(validate=not_blank)
  84.  
  85.     # self links
  86.     def get_top_level_links(self, data, many):
  87.         if many:
  88.             self_link = "/borg/process"
  89.         else:
  90.             self_link = "/borg/process/{}".format(data['id'])
  91.         return {'self': self_link}
  92.  
  93.     class Meta:
  94.         type_ = 'borg_process'
  95.         strict = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement