File tree Expand file tree Collapse file tree 4 files changed +23
-19
lines changed
Expand file tree Collapse file tree 4 files changed +23
-19
lines changed Original file line number Diff line number Diff line change 44from .db import db , migrate
55from .models import character , greeting
66
7+
78def create_app (test_config = None ):
89 app = Flask (__name__ )
910
1011 if not test_config :
1112 db_to_use = os .environ .get ("SQLALCHEMY_DATABASE_URI" )
1213 else :
1314 db_to_use = os .environ .get ("SQLALCHEMY_TEST_DATABASE_URI" )
14-
1515
1616 app .config ['SQLALCHEMY_TRACK_MODIFICATIONS' ] = False
1717 app .config ['SQLALCHEMY_DATABASE_URI' ] = db_to_use
@@ -21,4 +21,4 @@ def create_app(test_config=None):
2121
2222 app .register_blueprint (bp )
2323
24- return app
24+ return app
Original file line number Diff line number Diff line change 11from sqlalchemy .orm import DeclarativeBase
22
3+
34class Base (DeclarativeBase ):
4- pass
5+ pass
Original file line number Diff line number Diff line change 11from sqlalchemy .orm import Mapped , mapped_column , relationship
22from ..db import db
33
4+
45class Character (db .Model ):
56 id : Mapped [int ] = mapped_column (primary_key = True , autoincrement = True )
67 name : Mapped [str ]
78 personality : Mapped [str ]
89 occupation : Mapped [str ]
910 age : Mapped [int ]
1011 greetings : Mapped [list ["Greeting" ]] = relationship (back_populates = "character" )
11-
12+
1213 def to_dict (self ):
1314 return {
14- "id" : self .id ,
15- "name" : self .name ,
16- "personality" : self .personality ,
17- "occupation" : self .occupation ,
18- "age" : self .age
15+ "id" : self .id ,
16+ "name" : self .name ,
17+ "personality" : self .personality ,
18+ "occupation" : self .occupation ,
19+ "age" : self .age
1920 }
20-
21+
2122 @classmethod
2223 def from_dict (cls , data_dict ):
2324 new_character = cls (
24- name = data_dict ["name" ],
25- personality = data_dict ["personality" ],
26- occupation = data_dict ["occupation" ],
27- age = data_dict ["age" ]
25+ name = data_dict ["name" ],
26+ personality = data_dict ["personality" ],
27+ occupation = data_dict ["occupation" ],
28+ age = data_dict ["age" ]
2829 )
2930
3031 return new_character
Original file line number Diff line number Diff line change 22from sqlalchemy import ForeignKey
33from ..db import db
44
5+
56class Greeting (db .Model ):
67 id : Mapped [int ] = mapped_column (primary_key = True , autoincrement = True )
78 greeting_text : Mapped [str ]
8- character_id : Mapped [int ] = mapped_column (ForeignKey ("character.id" ))
9+ character_id : Mapped [int ] = mapped_column (ForeignKey ("character.id" ))
910 character : Mapped ["Character" ] = relationship (back_populates = "greetings" )
1011
12+
1113def to_dict (self ):
12- return {
13- "id" : self .id ,
14- "greeting" : self .greeting_text ,
15- }
14+ return {
15+ "id" : self .id ,
16+ "greeting" : self .greeting_text ,
17+ }
You can’t perform that action at this time.
0 commit comments