Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Miscellaneous
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
practice problem folder
Miscellaneous
Commits
aa66962e
Commit
aa66962e
authored
1 month ago
by
Duncan Holmes
Browse files
Options
Downloads
Patches
Plain Diff
finished ORM lab. we are forgiven
parent
78a04f6d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lab on ORM/main.py
+69
-37
69 additions, 37 deletions
lab on ORM/main.py
with
69 additions
and
37 deletions
lab on ORM/main.py
+
69
−
37
View file @
aa66962e
...
...
@@ -19,6 +19,44 @@ class MoviesApp(App):
self
.
movie_database
=
MovieDatabase
(
url
)
self
.
session
=
self
.
movie_database
.
create_session
()
def
get_selected_genres
(
self
,
action_selected
,
adventure_selected
,
sci_fi_selected
,
session
):
selected_genres
=
[]
if
action_selected
:
# Query for the existing "Action" genre in the database
action_genre
=
session
.
query
(
Genre
).
filter_by
(
name
=
'
Action
'
).
first
()
if
not
action_genre
:
# If the genre doesn't exist, create it
action_genre
=
Genre
(
name
=
'
Action
'
)
selected_genres
.
append
(
action_genre
)
if
adventure_selected
:
adventure_genre
=
session
.
query
(
Genre
).
filter_by
(
name
=
'
Adventure
'
).
first
()
if
not
adventure_genre
:
adventure_genre
=
Genre
(
name
=
'
Adventure
'
)
selected_genres
.
append
(
adventure_genre
)
if
sci_fi_selected
:
sci_fi_genre
=
session
.
query
(
Genre
).
filter_by
(
name
=
'
SciFi
'
).
first
()
if
not
sci_fi_genre
:
sci_fi_genre
=
Genre
(
name
=
'
SciFi
'
)
selected_genres
.
append
(
sci_fi_genre
)
return
selected_genres
def
display_database
(
self
,
session
):
# Fetch and print the entire database: Movies and their genres
print
(
"
\n
---------- Entire Database: Movies and Genres ----------
"
)
all_movies
=
session
.
query
(
Movie
).
all
()
for
movie
in
all_movies
:
print
(
f
"
Movie ID:
{
movie
.
movie_id
}
, Title:
{
movie
.
title
}
"
)
print
(
f
"
Genres:
{
[
genre
.
name
for
genre
in
movie
.
genres
]
}
"
)
print
(
"
--------------------------------------------------------
\n
"
)
def
update_text_below_rest_of_interface
(
self
,
text
,
*
args
):
self
.
root
.
ids
.
message
.
text
=
text
def
clear_text_below_rest_of_interface
(
self
,
*
args
):
self
.
root
.
ids
.
message
.
text
=
""
def
create_movie
(
self
,
title
,
action_selected
,
adventure_selected
,
sci_fi_selected
):
if
(
title
==
"
peepeepoopoo_1
"
):
query
=
self
.
session
.
query
(
Genre
)
...
...
@@ -83,20 +121,6 @@ class MoviesApp(App):
self
.
update_text_below_rest_of_interface
(
text
=
f
'
{
title
}
has been added to the database.
'
)
Clock
.
schedule_once
(
self
.
clear_text_below_rest_of_interface
,
5
)
def
display_database
(
self
,
session
):
# Fetch and print the entire database: Movies and their genres
print
(
"
\n
---------- Entire Database: Movies and Genres ----------
"
)
all_movies
=
session
.
query
(
Movie
).
all
()
for
movie
in
all_movies
:
print
(
f
"
Movie ID:
{
movie
.
movie_id
}
, Title:
{
movie
.
title
}
"
)
print
(
f
"
Genres:
{
[
genre
.
name
for
genre
in
movie
.
genres
]
}
"
)
print
(
"
--------------------------------------------------------
\n
"
)
def
update_text_below_rest_of_interface
(
self
,
text
,
*
args
):
self
.
root
.
ids
.
message
.
text
=
text
def
clear_text_below_rest_of_interface
(
self
,
*
args
):
self
.
root
.
ids
.
message
.
text
=
""
def
update_movie
(
self
,
title
,
action_selected
,
adventure_selected
,
sci_fi_selected
):
session
=
self
.
movie_database
.
create_session
()
# Write code in update_movie to find the movie with the title entered by the user.
...
...
@@ -118,35 +142,43 @@ class MoviesApp(App):
# them to appear in the database.)
# i suppose .all returns a list. this poses a design question i will ignore.
# the design equesiton being if we're allowing duplicates. of that i will ignore
session
.
query
(
Movie
).
filter_by
(
title
=
title
).
all
()[
0
].
genres
=
selected_genres
self
.
display_database
(
session
)
if
session
.
query
(
Movie
).
filter_by
(
title
=
title
).
count
()
<=
0
:
self
.
update_text_below_rest_of_interface
(
text
=
f
'
{
title
}
is not in the database.
'
)
else
:
session
.
query
(
Movie
).
filter_by
(
title
=
title
).
all
()[
0
].
genres
=
selected_genres
session
.
commit
()
self
.
display_database
(
session
)
self
.
update_text_below_rest_of_interface
(
text
=
f
'
{
title
}
has been updated in the database.
'
)
def
get_selected_genres
(
self
,
action_selected
,
adventure_selected
,
sci_fi_selected
,
session
):
selected_genres
=
[]
Clock
.
schedule_once
(
self
.
clear_text_below_rest_of_interface
,
5
)
if
action_selected
:
# Query for the existing "Action" genre in the database
action_genre
=
session
.
query
(
Genre
).
filter_by
(
name
=
'
Action
'
).
first
()
if
not
action_genre
:
# If the genre doesn't exist, create it
action_genre
=
Genre
(
name
=
'
Action
'
)
selected_genres
.
append
(
action_genre
)
if
adventure_selected
:
adventure_genre
=
session
.
query
(
Genre
).
filter_by
(
name
=
'
Adventure
'
).
first
()
if
not
adventure_genre
:
adventure_genre
=
Genre
(
name
=
'
Adventure
'
)
selected_genres
.
append
(
adventure_genre
)
if
sci_fi_selected
:
sci_fi_genre
=
session
.
query
(
Genre
).
filter_by
(
name
=
'
SciFi
'
).
first
()
if
not
sci_fi_genre
:
sci_fi_genre
=
Genre
(
name
=
'
SciFi
'
)
selected_genres
.
append
(
sci_fi_genre
)
def
delete_movie
(
self
,
title
):
#Queries have a method .count() that counts the matching rows;
# it provides less information than the .one() method, but also has better
# performance. Using the count method, write code to throw a ValueError
# if no movies have the title the user entered.
session
=
self
.
movie_database
.
create_session
()
#rememebr this line it's the most badass line ever
movie
=
session
.
query
(
Movie
).
filter_by
(
title
=
title
).
first
()
if
not
movie
:
#wack ass python conditional
self
.
update_text_below_rest_of_interface
(
text
=
f
'
{
title
}
is not in the database.
'
)
Clock
.
schedule_once
(
self
.
clear_text_below_rest_of_interface
,
5
)
return
#.clear() is a list method
movie
.
genres
.
clear
()
# 0_0
session
.
delete
(
movie
)
self
.
display_database
(
session
)
self
.
update_text_below_rest_of_interface
(
text
=
f
'
{
title
}
has been deleted. rest in pepperonis.
'
)
return
selected_genres
def
delete_movie
(
self
,
title
):
pass
def
main
():
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment