Global configurations for the diffgram installation.
| 6 | |
| 7 | |
| 8 | class SystemConfigs(Base): |
| 9 | """ |
| 10 | Global configurations for the diffgram installation. |
| 11 | """ |
| 12 | __tablename__ = 'system_configs' |
| 13 | id = Column(BIGINT, primary_key = True) |
| 14 | |
| 15 | logo_id = Column(Integer, ForeignKey('image.id')) |
| 16 | logo = relationship("Image") |
| 17 | |
| 18 | |
| 19 | def serialize(self, session): |
| 20 | logo_data = None |
| 21 | if self.logo_id: |
| 22 | logo_data = self.logo.serialize_for_source_control(session = session, regen_url = True) |
| 23 | return { |
| 24 | 'id': self.id, |
| 25 | 'logo_id': self.logo_id, |
| 26 | 'logo': logo_data |
| 27 | } |
| 28 | |
| 29 | @staticmethod |
| 30 | def set_logo(session, image_id: int): |
| 31 | """ |
| 32 | Saves the config blob path from logo and generates signed URL. |
| 33 | :param session: |
| 34 | :param blob_path: |
| 35 | :return: Updated configs object |
| 36 | """ |
| 37 | configs = SystemConfigs.get_configs(session = session) |
| 38 | if not configs: |
| 39 | configs = SystemConfigs.new(session = session) |
| 40 | configs.logo_id = image_id |
| 41 | session.add(configs) |
| 42 | return configs |
| 43 | @staticmethod |
| 44 | def get_configs(session): |
| 45 | result = session.query(SystemConfigs).first() |
| 46 | if not result: |
| 47 | result = SystemConfigs.new(session = session) |
| 48 | return result |
| 49 | @staticmethod |
| 50 | def new(session, |
| 51 | logo_id: int = None, |
| 52 | add_to_session = True, |
| 53 | flush_session = True): |
| 54 | |
| 55 | configs = SystemConfigs( |
| 56 | logo_id = logo_id, |
| 57 | ) |
| 58 | if add_to_session: |
| 59 | session.add(configs) |
| 60 | if flush_session: |
| 61 | session.flush() |
| 62 | |
| 63 | # Try commit |
| 64 | commit_with_rollback(session) |
| 65 |