def __create_by_role(cls, staff_id, role): level_relation = merge(cls.get_level_relation(staff_id, role.level)) relation = merge(cls.get_relation(staff_id, role.role_id)) if relation is None and level_relation is None: relation = cls(staff_id=staff_id, role_id=role.role_id) relation.add() cache.invalidate(cls.get_relation, 'get_relation_staff_role', staff_id, role.role_id) cache.invalidate(cls.get_level_relation, 'get_level_relation_staff_role', staff_id, role.level) cache.invalidate(cls.get_relations_by_staff, 'get_relations_role_by_staff', staff_id) return relation
def create_by_name(cls, staff_id, role_name): from ..entity.role import Role role = merge(Role.get_role_by_name(role_name.lower())) if role: return cls.__create_by_role(staff_id, role) return None
def create_by_id(cls, staff_id, role_id): from ..entity.role import Role role = merge(Role.get_role_by_id(role_id)) if role: return cls.__create_by_role(staff_id, role) return None
def create(cls, data): staff = None if 'staff_id' in data: staff = merge(cls.get_staff_by_id(data['staff_id'])) if staff: return staff staff = cls(name=data['name'], surname=data['surname'], bio=data['bio']) staff.add() staff.last_change_by_id = data.get('last_change_by_id', staff.staff_id) staff.add() cls._invalidate_cache(staff) role_relations = StaffRole.create_relations_by_staff( staff.staff_id, data['roles']) if len(role_relations) == 0: staff.delete_self() cls._invalidate_cache(staff) return None return staff
def __delete_relation(cls, relation): from ..entity.role import Role relation.delete_self() role = merge(Role.get_role_by_id(relation.role_id)) cache.invalidate(cls.get_relation, 'get_relation_staff_role', relation.staff_id, relation.role_id) cache.invalidate(cls.get_level_relation, 'get_level_relation_staff_role', relation.staff_id, role.level)
def from_dict(cls, data): item = cls() for field in cls.__table__.columns.keys(): if field in data: if cls.__table__.columns[field].type.python_type == time: if validate_iso8601(data[field]): setattr(item, field, parser.parse(data[field]).time()) elif cls.__table__.columns[field].type.python_type == datetime: if validate_iso8601(data[field]): setattr(item, field, parser.parse(data[field])) else: setattr(item, field, data[field]) return merge(item)
def delete(cls, staff_id): staff = merge(cls.get_staff_by_id(staff_id)) if staff: cls._delete(staff) return staff return None
def create_all(*args, **kwargs): admin_id = merge(Role.get_role_by_name('admin')).role_id admins_data = get_init_db()['admins'] for admin_data in admins_data: admin_data['roles'] = [admin_id] Staff.create(admin_data)