|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import datetime |
| 4 | +import uuid |
| 5 | + |
| 6 | +from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, event |
| 7 | +from sqlalchemy.ext.hybrid import hybrid_property |
| 8 | +from sqlalchemy.orm import relationship |
| 9 | + |
| 10 | +import bleach |
| 11 | +import characterentities |
| 12 | + |
| 13 | +from .base import Base |
| 14 | + |
| 15 | + |
| 16 | +class Badge(Base): |
| 17 | + __tablename__ = 'gk-badges-collection' |
| 18 | + |
| 19 | + id = Column( |
| 20 | + 'id', |
| 21 | + Integer, |
| 22 | + primary_key=True, |
| 23 | + key='id', |
| 24 | + ) |
| 25 | + |
| 26 | + _name = Column( |
| 27 | + 'name', |
| 28 | + String(80), |
| 29 | + key='name', |
| 30 | + nullable=False, |
| 31 | + unique=True, |
| 32 | + ) |
| 33 | + |
| 34 | + _description = Column( |
| 35 | + 'description', |
| 36 | + String(128), |
| 37 | + key='description', |
| 38 | + nullable=True, |
| 39 | + ) |
| 40 | + |
| 41 | + filename = Column( |
| 42 | + 'filename', |
| 43 | + String(32), |
| 44 | + key='filename', |
| 45 | + nullable=False, |
| 46 | + ) |
| 47 | + |
| 48 | + created_on_datetime = Column( |
| 49 | + 'date', |
| 50 | + DateTime, |
| 51 | + key='created_on_datetime', |
| 52 | + nullable=False, |
| 53 | + default=datetime.datetime.utcnow, |
| 54 | + ) |
| 55 | + |
| 56 | + author_id = Column( |
| 57 | + 'author_id', |
| 58 | + Integer, |
| 59 | + ForeignKey('gk-users.id'), |
| 60 | + key='author_id', |
| 61 | + nullable=False, |
| 62 | + ) |
| 63 | + |
| 64 | + author = relationship( |
| 65 | + "User", |
| 66 | + foreign_keys=[author_id], |
| 67 | + backref="created_badges" |
| 68 | + ) |
| 69 | + |
| 70 | + @hybrid_property |
| 71 | + def name(self): |
| 72 | + return characterentities.decode(self._name) |
| 73 | + |
| 74 | + @name.setter |
| 75 | + def name(self, name): |
| 76 | + name_clean = bleach.clean(name, strip=True) |
| 77 | + self._name = characterentities.decode(name_clean).strip() |
| 78 | + |
| 79 | + @name.expression |
| 80 | + def name(cls): |
| 81 | + return cls._name |
| 82 | + |
| 83 | + @hybrid_property |
| 84 | + def description(self): |
| 85 | + if self._description is None: |
| 86 | + return u'' |
| 87 | + return characterentities.decode(self._description) |
| 88 | + |
| 89 | + @description.setter |
| 90 | + def description(self, description): |
| 91 | + if description is None: |
| 92 | + return u'' |
| 93 | + description_clean = bleach.clean(description, strip=True) |
| 94 | + self._description = characterentities.decode(description_clean).replace('\x00', '').strip() |
| 95 | + |
| 96 | + @description.expression |
| 97 | + def description(cls): |
| 98 | + return cls._description |
| 99 | + |
| 100 | + @hybrid_property |
| 101 | + def upload_url(self): |
| 102 | + # Todo send info to other daemon -> redis? |
| 103 | + from app.views.minio import storage |
| 104 | + res = storage.connection.presigned_put_object( |
| 105 | + 'badges-incoming', self.filename) # , expires=timedelta(minutes=5)) |
| 106 | + return res |
| 107 | + # return uuid.uuid4().hex |
| 108 | + |
| 109 | + |
| 110 | +@event.listens_for(Badge, 'init') |
| 111 | +def receive_init(target, args, kwargs): |
| 112 | + target.filename = uuid.uuid4().hex |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + # Check |
| 117 | + badge = Badge() |
0 commit comments