66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, ForeignKey, Index, TIMESTAMP
|
|
from sqlalchemy.orm import relationship, validates
|
|
from src.organizationtypes.model import Organizationtype
|
|
from datetime import datetime
|
|
from src.database import Base,db_session
|
|
from marshmallow import Schema, fields, post_load, ValidationError
|
|
from src import clogger
|
|
import json
|
|
import flask
|
|
#from src.models import Section
|
|
import re
|
|
|
|
def validate_key(key):
|
|
if re.search('[\s)(?*/&+?]+', key) is not None:
|
|
raise ValidationError("Special Character not allowed")
|
|
if key.strip()=="":
|
|
raise ValidationError("No empty keys allowed")
|
|
return key
|
|
|
|
class OrganizationtypeCompSchema(Schema):
|
|
id =fields.Integer()
|
|
name=fields.String()
|
|
|
|
class FullOrganizationSchema(Schema):
|
|
id = fields.Integer()
|
|
key=fields.String(validate=validate_key)
|
|
url = fields.String()
|
|
name = fields.String()
|
|
image = fields.String()
|
|
updated_at = fields.DateTime()
|
|
created_at = fields.DateTime()
|
|
# organizationtype_id = fields.Integer()
|
|
organizationtype=fields.Nested(OrganizationtypeCompSchema)
|
|
|
|
class Organization(Base):
|
|
__tablename__ = 'organizations'
|
|
url = Column(String(250))
|
|
name=Column(String(250),nullable=False)
|
|
key=Column(String(250), unique=True, nullable=False)
|
|
Index("key", "key")
|
|
sections=relationship("Section", back_populates="organization")
|
|
image=Column(String(250))
|
|
organizationtype_id=Column(Integer, ForeignKey('organizationtypes.id'))
|
|
organizationtype=relationship("Organizationtype")
|
|
text = Column(Text)
|
|
__schema__= FullOrganizationSchema
|
|
__jsonid__= 'organization'
|
|
__whiteattrs__= ["url", "name", "image","key", "organizationtype_id"]
|
|
__jsonattrs__= ["id", "url", "name", "updated_at", "created_at", "image", "key","organizationtype.name","text" ] # None means all
|
|
|
|
@validates('key')
|
|
def _validate_key(self,k, key):
|
|
return validate_key(key)
|
|
def __init__(self, data={}):
|
|
self.update(data,False)
|
|
|
|
|
|
|
|
|
|
|
|
#class OrganizationSchema(Schema):
|
|
# id = fields.Integer()
|
|
# url = fields.String()
|
|
# name = fields.String()
|
|
# image = fields.String()
|