Sitemap generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.3 KiB

4 years ago
  1. from sqlalchemy import Column, Integer, String, Boolean, ForeignKey, Table, exists
  2. from sqlalchemy.orm import relationship
  3. import hashlib
  4. from db import Model, db_endine, session
  5. import uuid
  6. from validators import domain as domain_validator
  7. groups_domains = Table(
  8. 'groups_domains',
  9. Model.metadata,
  10. Column('domain_id', Integer, ForeignKey('domain_groups.id')),
  11. Column('group_id', Integer, ForeignKey('domains.id'))
  12. )
  13. class DomainGroup(Model):
  14. __tablename__ = 'domain_groups'
  15. id = Column(Integer, primary_key=True)
  16. name = Column(String(200), nullable=False)
  17. domains = relationship("Domain", secondary=groups_domains, back_populates="groups")
  18. def __init__(self, name):
  19. self.name = name
  20. def __repr__(self):
  21. return "<Domain group {}: {}>".format(self.id, self.name)
  22. @classmethod
  23. def from_json(cls, data):
  24. return cls(**data)
  25. class Domain(Model):
  26. __tablename__ = 'domains'
  27. id = Column(Integer, primary_key=True)
  28. domain = Column(String(200), nullable=False)
  29. domains = relationship("DomainGroup", secondary=groups_domains, back_populates="domains")
  30. def __init__(self, domain):
  31. self.validate_domain(domain)
  32. self.domain = domain
  33. def validate_domain(self, domain=None):
  34. domain = domain or self.domain
  35. return domain_validator(domain, raise_errors=True)
  36. def __repr__(self):
  37. return "<Domain {}: {}>".format(self.id, self.domain)
  38. @classmethod
  39. def from_json(cls, data):
  40. return cls(**data)
  41. class User(Model):
  42. __tablename__ = 'users'
  43. id = Column(Integer, primary_key=True)
  44. username = Column(String(50), unique=True)
  45. token = Column(String(250), default='Unknown')
  46. is_active = Column(Boolean, default=False)
  47. def __init__(self, username: str):
  48. self.username = username
  49. m = hashlib.sha256()
  50. m.update('{}{}'.format(username, uuid.uuid4()).encode('utf-8'))
  51. self.token = m.hexdigest()
  52. @classmethod
  53. def validate(cls, username, token):
  54. return session.query(cls).filter(cls.username == username, cls.token == token).count() == 1
  55. def __repr__(self):
  56. return "<User(name='%s', fullname='%s', password='%s')>" % (self.name, self.fullname, self.password)
  57. Model.metadata.create_all(db_endine)