Skip to content

Commit a2c18e6

Browse files
authored
Merge pull request #34 from 4GeeksAcademy/modeling
modelin y vistas de panel de admin
2 parents d3712c0 + 6b6725c commit a2c18e6

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

migrations/versions/9ea39839a29b_.py migrations/versions/49ae031ded2d_.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
"""empty message
22
3-
Revision ID: 9ea39839a29b
3+
Revision ID: 49ae031ded2d
44
Revises:
5-
Create Date: 2025-03-16 15:09:02.935381
5+
Create Date: 2025-03-16 17:16:41.870761
66
77
"""
88
from alembic import op
99
import sqlalchemy as sa
1010

1111

1212
# revision identifiers, used by Alembic.
13-
revision = '9ea39839a29b'
13+
revision = '49ae031ded2d'
1414
down_revision = None
1515
branch_labels = None
1616
depends_on = None
@@ -23,7 +23,9 @@ def upgrade():
2323
sa.Column('name', sa.String(length=120), nullable=False),
2424
sa.Column('email', sa.String(length=120), nullable=False),
2525
sa.Column('password', sa.String(length=500), nullable=False),
26+
sa.Column('gender', sa.Enum('MALE', 'FEMALE', 'NON_BINARY', 'OTHER', 'PREFER_NOT_TO_SAY', name='gender'), nullable=False),
2627
sa.Column('is_active', sa.Boolean(), nullable=False),
28+
sa.Column('role', sa.Enum('ADMIN', 'MODERATOR', 'USER', name='role'), nullable=False),
2729
sa.PrimaryKeyConstraint('id'),
2830
sa.UniqueConstraint('email')
2931
)

src/api/admin.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
21
import os
32
from flask_admin import Admin
43
from .models import db, User, Notes, Habits, Goals, Projects
54
from flask_admin.contrib.sqla import ModelView
65

6+
class UserView(ModelView):
7+
column_list = ('name', 'email','password', 'gender', 'is_active', 'role')
8+
form_columns = ('name', 'email','password', 'gender', 'is_active', 'role')
9+
710
class NotesView(ModelView):
811
column_list = ('title', 'description','category', 'user_id', 'projects_id')
912
form_columns = ('title', 'description','category', 'user_id', 'projects_id')
1013

1114
class HabitsView(ModelView):
12-
column_list = ('name', 'description','category', 'user_id', 'goals_id', 'ready')
13-
form_columns = ('name', 'description','category', 'user_id', 'goals_id','ready')
15+
column_list = ('name', 'description', 'category', 'ready', 'user_id', 'goals_id')
16+
form_columns = ('name', 'description', 'category', 'ready', 'user_id', 'goals_id')
1417

1518
class GoalsView(ModelView):
16-
column_list = ('target', 'description' ,'user_id', 'projects_id', 'ready')
17-
form_columns = ('target', 'description','user_id', 'projects_id', 'ready')
19+
column_list = ('target', 'description', 'ready', 'user_id', 'projects_id')
20+
form_columns = ('target', 'description', 'ready', 'user_id', 'projects_id')
1821

1922
class ProjectsView(ModelView):
2023
column_list = ('name', 'description', 'category', 'user_id')
@@ -28,7 +31,7 @@ def setup_admin(app):
2831

2932

3033
# Add your models here, for example this is how we add a the User model to the admin
31-
admin.add_view(ModelView(User, db.session))
34+
admin.add_view(UserView(User, db.session))
3235
admin.add_view(NotesView(Notes, db.session))
3336
admin.add_view(HabitsView(Habits, db.session))
3437
admin.add_view(GoalsView(Goals, db.session))

src/api/models.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
from flask_sqlalchemy import SQLAlchemy
22
from sqlalchemy.orm import mapped_column, Mapped, relationship
3-
from sqlalchemy import Integer, String, ForeignKey
3+
from sqlalchemy import Integer, Enum, String, ForeignKey, Boolean
4+
from enum import Enum as PyEnum
45

56
db = SQLAlchemy()
67

8+
class Gender(PyEnum):
9+
MALE = "Male"
10+
FEMALE = "Female"
11+
NON_BINARY = "Non-binary"
12+
OTHER = "Other"
13+
PREFER_NOT_TO_SAY = "Prefer not to say"
14+
15+
class Role(PyEnum):
16+
ADMIN = "Admin"
17+
MODERATOR = "Moderator"
18+
USER = "User"
19+
720
class User(db.Model):
821
__tablename__ = "user"
922

1023
id: Mapped[int] = mapped_column(primary_key=True)
1124
name: Mapped[str] = mapped_column(String(120), nullable=False)
1225
email: Mapped[str] = mapped_column(String(120), unique=True, nullable=False)
1326
password: Mapped[str] = mapped_column(String(500))
14-
is_active: Mapped[bool]
15-
# role: Mapped[int] = mapped_column(Integer, nullable=False)
27+
gender: Mapped[Gender] = mapped_column(Enum(Gender), nullable=False)
28+
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
29+
role: Mapped[Role] = mapped_column(Enum(Role), nullable=False, default=Role.USER)
1630
notes: Mapped["Notes"] = relationship(back_populates="user")
1731
habits: Mapped["Habits"] = relationship(back_populates="user")
1832
goals: Mapped["Goals"] = relationship(back_populates="user")
@@ -24,8 +38,9 @@ def serialize(self):
2438
"id": self.id,
2539
"name": self.name,
2640
"email": self.email,
41+
"gender": self.gender.value,
2742
"is_active": self.is_active,
28-
# "role": self.role
43+
"role": self.role.value
2944
}
3045

3146
class Notes(db.Model):

0 commit comments

Comments
 (0)