Skip to content

Commit

Permalink
Added README file and query_movies.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike014 committed Jan 18, 2025
1 parent 76c553d commit 26bf550
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Follow these steps to set up and run **MovieRanker** locally:
```

OR

```bash
python manage.py dbshell
```
Expand Down
70 changes: 46 additions & 24 deletions movies/management/commands/query_movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,75 @@
from django.core.management.base import BaseCommand
from movies.models import Movie


class Command(BaseCommand):
help = 'Query movies from the database with different query types, insert a new movie, and delete a movie'
help = "Query movies from the database with different query types, insert a new movie, and delete a movie"

def add_arguments(self, parser):
parser.add_argument('--type', type=str, help='Type of query: all, filter, exclude, insert, delete', required=True)
parser.add_argument('--field', type=str, help='Field to filter, exclude, or delete by')
parser.add_argument('--value', type=str, help='Value to filter, exclude, or delete by')
parser.add_argument('--title', type=str, help='Title of the movie to insert')
parser.add_argument('--director', type=str, help='Director of the movie to insert')
parser.add_argument('--year', type=int, help='Year of the movie to insert')
parser.add_argument(
"--type",
type=str,
help="Type of query: all, filter, exclude, insert, delete",
required=True,
)
parser.add_argument(
"--field", type=str, help="Field to filter, exclude, or delete by"
)
parser.add_argument(
"--value", type=str, help="Value to filter, exclude, or delete by"
)
parser.add_argument("--title", type=str, help="Title of the movie to insert")
parser.add_argument(
"--director", type=str, help="Director of the movie to insert"
)
parser.add_argument("--year", type=int, help="Year of the movie to insert")

def handle(self, *args, **kwargs):
query_type = kwargs['type']
field = kwargs.get('field')
value = kwargs.get('value')
title = kwargs.get('title')
director = kwargs.get('director')
year = kwargs.get('year')
query_type = kwargs["type"]
field = kwargs.get("field")
value = kwargs.get("value")
title = kwargs.get("title")
director = kwargs.get("director")
year = kwargs.get("year")

if query_type == 'all':
if query_type == "all":
self.stdout.write("Querying all movies from the database:")
movies = Movie.objects.all()
elif query_type == 'filter' and field and value:
self.stdout.write(f"Querying movies from the database where {field} is {value}:")
elif query_type == "filter" and field and value:
self.stdout.write(
f"Querying movies from the database where {field} is {value}:"
)
filter_kwargs = {field: value}
movies = Movie.objects.filter(**filter_kwargs)
elif query_type == 'exclude' and field and value:
self.stdout.write(f"Querying movies from the database excluding where {field} is {value}:")
elif query_type == "exclude" and field and value:
self.stdout.write(
f"Querying movies from the database excluding where {field} is {value}:"
)
exclude_kwargs = {field: value}
movies = Movie.objects.exclude(**exclude_kwargs)
elif query_type == 'insert' and title and director and year:
self.stdout.write(f"Inserting a new movie: {title}, directed by {director}, released in {year}")
elif query_type == "insert" and title and director and year:
self.stdout.write(
f"Inserting a new movie: {title}, directed by {director}, released in {year}"
)
new_movie = Movie(title=title, director=director, year=year)
new_movie.save()
self.stdout.write(f"Movie '{title}' inserted successfully.")
return
elif query_type == 'delete' and field and value:
self.stdout.write(f"Deleting movies from the database where {field} is {value}:")
elif query_type == "delete" and field and value:
self.stdout.write(
f"Deleting movies from the database where {field} is {value}:"
)
delete_kwargs = {field: value}
movies_to_delete = Movie.objects.filter(**delete_kwargs)
count, _ = movies_to_delete.delete()
self.stdout.write(f"Deleted {count} movie(s) where {field} is {value}.")
return
else:
self.stdout.write("Invalid query type or missing field/value for filter/exclude/delete, or missing details for insert.")
self.stdout.write(
"Invalid query type or missing field/value for filter/exclude/delete, or missing details for insert."
)
return

for movie in movies:
self.stdout.write(movie.title)
print(movie.title)
print(movie.title)

0 comments on commit 26bf550

Please sign in to comment.