Model representing an organization. Attributes: id (Integer): The primary key of the organization. name (String): The name of the organization. description (String): The description of the organization.
| 5 | |
| 6 | |
| 7 | class Organisation(DBBaseModel): |
| 8 | """ |
| 9 | Model representing an organization. |
| 10 | |
| 11 | Attributes: |
| 12 | id (Integer): The primary key of the organization. |
| 13 | name (String): The name of the organization. |
| 14 | description (String): The description of the organization. |
| 15 | """ |
| 16 | |
| 17 | __tablename__ = 'organisations' |
| 18 | |
| 19 | id = Column(Integer, primary_key=True) |
| 20 | name = Column(String) |
| 21 | description = Column(String) |
| 22 | |
| 23 | def __repr__(self): |
| 24 | """ |
| 25 | Returns a string representation of the Organisation object. |
| 26 | |
| 27 | Returns: |
| 28 | str: String representation of the Organisation object. |
| 29 | """ |
| 30 | |
| 31 | return f"Organisation(id={self.id}, name='{self.name}')" |
| 32 | |
| 33 | @classmethod |
| 34 | def find_or_create_organisation(cls, session, user): |
| 35 | """ |
| 36 | Finds or creates an organization for the given user. |
| 37 | |
| 38 | Args: |
| 39 | session: The database session. |
| 40 | user: The user object. |
| 41 | |
| 42 | Returns: |
| 43 | Organisation: The found or created organization. |
| 44 | """ |
| 45 | |
| 46 | if user.organisation_id is not None: |
| 47 | organisation = session.query(Organisation).filter(Organisation.id == user.organisation_id).first() |
| 48 | return organisation |
| 49 | |
| 50 | existing_organisation = session.query(Organisation).filter( |
| 51 | Organisation.name == "Default Organization - " + str(user.id)).first() |
| 52 | |
| 53 | if existing_organisation is not None: |
| 54 | user.organisation_id = existing_organisation.id |
| 55 | session.commit() |
| 56 | return existing_organisation |
| 57 | new_organisation = Organisation( |
| 58 | name="Default Organization - " + str(user.id), |
| 59 | description="New default organiztaion", |
| 60 | ) |
| 61 | |
| 62 | session.add(new_organisation) |
| 63 | session.commit() |
| 64 | session.flush() |
no outgoing calls