|
| 1 | +from datetime import datetime |
| 2 | + |
1 | 3 | from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String |
2 | 4 | from sqlalchemy.orm import relationship |
3 | 5 |
|
4 | | -from .database import Base |
| 6 | +from app.database.database import Base |
| 7 | + |
| 8 | + |
| 9 | +class UserEvent(Base): |
| 10 | + __tablename__ = "user_event" |
| 11 | + |
| 12 | + id = Column(Integer, primary_key=True, index=True) |
| 13 | + user_id = Column('user_id', Integer, ForeignKey('users.id')) |
| 14 | + event_id = Column('event_id', Integer, ForeignKey('events.id')) |
| 15 | + |
| 16 | + events = relationship("Event", back_populates="participants") |
| 17 | + participants = relationship("User", back_populates="events") |
| 18 | + |
| 19 | + def __repr__(self): |
| 20 | + return f'<UserEvent ({self.participants}, {self.events})>' |
5 | 21 |
|
6 | 22 |
|
7 | 23 | class User(Base): |
8 | 24 | __tablename__ = "users" |
9 | 25 |
|
10 | 26 | id = Column(Integer, primary_key=True, index=True) |
11 | | - username = Column(String, unique=True) |
12 | | - email = Column(String, unique=True) |
13 | | - password = Column(String) |
| 27 | + username = Column(String, unique=True, nullable=False) |
| 28 | + email = Column(String, unique=True, nullable=False) |
| 29 | + password = Column(String, nullable=False) |
14 | 30 | full_name = Column(String) |
15 | 31 | description = Column(String, default="Happy new user!") |
16 | 32 | avatar = Column(String, default="profile.png") |
| 33 | + is_active = Column(Boolean, default=False) |
17 | 34 |
|
18 | | - is_active = Column(Boolean, default=True) |
| 35 | + events = relationship("UserEvent", back_populates="participants") |
19 | 36 |
|
20 | | - events = relationship( |
21 | | - "Event", cascade="all, delete", back_populates="owner") |
| 37 | + def __repr__(self): |
| 38 | + return f'<User {self.id}>' |
22 | 39 |
|
23 | 40 |
|
24 | 41 | class Event(Base): |
25 | 42 | __tablename__ = "events" |
26 | 43 |
|
27 | 44 | id = Column(Integer, primary_key=True, index=True) |
28 | | - title = Column(String) |
29 | | - content = Column(String) |
| 45 | + title = Column(String, nullable=False) |
30 | 46 | start = Column(DateTime, nullable=False) |
31 | 47 | end = Column(DateTime, nullable=False) |
| 48 | + content = Column(String) |
| 49 | + location = Column(String) |
| 50 | + |
| 51 | + owner = relationship("User") |
32 | 52 | owner_id = Column(Integer, ForeignKey("users.id")) |
| 53 | + participants = relationship("UserEvent", back_populates="events") |
| 54 | + |
| 55 | + def __repr__(self): |
| 56 | + return f'<Event {self.id}>' |
| 57 | + |
| 58 | + |
| 59 | +class Invitation(Base): |
| 60 | + __tablename__ = "invitations" |
| 61 | + |
| 62 | + id = Column(Integer, primary_key=True, index=True) |
| 63 | + status = Column(String, nullable=False, default="unread") |
| 64 | + recipient_id = Column(Integer, ForeignKey("users.id")) |
| 65 | + event_id = Column(Integer, ForeignKey("events.id")) |
| 66 | + creation = Column(DateTime, default=datetime.now) |
| 67 | + |
| 68 | + recipient = relationship("User") |
| 69 | + event = relationship("Event") |
33 | 70 |
|
34 | | - owner = relationship("User", back_populates="events") |
| 71 | + def __repr__(self): |
| 72 | + return ( |
| 73 | + f'<Invitation ' |
| 74 | + f'({self.event.owner}' |
| 75 | + f'to {self.recipient})>' |
| 76 | + ) |
0 commit comments