-
Notifications
You must be signed in to change notification settings - Fork 899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Develop #934
base: master
Are you sure you want to change the base?
Develop #934
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Generated by Django 4.0.2 on 2024-11-10 13:51 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('db', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='movie', | ||
name='actors', | ||
field=models.ManyToManyField(related_name='movies', to='db.Actor'), | ||
), | ||
migrations.AlterField( | ||
model_name='movie', | ||
name='genres', | ||
field=models.ManyToManyField(related_name='movies', to='db.Genre'), | ||
), | ||
migrations.AlterField( | ||
model_name='moviesession', | ||
name='cinema_hall', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='movie_sessions', to='db.cinemahall'), | ||
), | ||
migrations.AlterField( | ||
model_name='moviesession', | ||
name='movie', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='movie_sessions', to='db.movie'), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Generated by Django 4.0.2 on 2024-11-10 14:10 | ||
|
||
from django.conf import settings | ||
import django.contrib.auth.models | ||
import django.contrib.auth.validators | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('auth', '0012_alter_user_first_name_max_length'), | ||
('db', '0002_alter_movie_actors_alter_movie_genres_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='User', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('password', models.CharField(max_length=128, verbose_name='password')), | ||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), | ||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), | ||
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')), | ||
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), | ||
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), | ||
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')), | ||
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), | ||
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), | ||
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), | ||
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), | ||
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), | ||
], | ||
options={ | ||
'verbose_name': 'user', | ||
'verbose_name_plural': 'users', | ||
'abstract': False, | ||
}, | ||
managers=[ | ||
('objects', django.contrib.auth.models.UserManager()), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='Order', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='orders', to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
'ordering': ['-created_at'], | ||
}, | ||
), | ||
migrations.AlterField( | ||
model_name='movie', | ||
name='title', | ||
field=models.CharField(db_index=True, max_length=255), | ||
), | ||
migrations.CreateModel( | ||
name='Ticket', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('row', models.IntegerField()), | ||
('seat', models.IntegerField()), | ||
('movie_session', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tickets', to='db.moviesession')), | ||
('order', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tickets', to='db.order')), | ||
], | ||
), | ||
migrations.AddConstraint( | ||
model_name='ticket', | ||
constraint=models.UniqueConstraint(fields=('row', 'seat', 'movie_session'), name='ticket_constraint'), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from datetime import datetime | ||
from django.contrib.auth import get_user_model | ||
from django.db import transaction | ||
from django.db.models import QuerySet | ||
from db.models import Order, Ticket | ||
|
||
|
||
@transaction.atomic | ||
def create_order( | ||
tickets: list[dict], | ||
username: str, | ||
date: datetime = None, | ||
) -> Order: | ||
user = get_user_model().objects.get(username=username) | ||
order = Order.objects.create( | ||
user=user | ||
) | ||
|
||
if date: | ||
order.created_at = date | ||
order.save() | ||
Comment on lines
+19
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting the |
||
|
||
for ticket in tickets: | ||
Ticket.objects.create( | ||
row=ticket.get("row"), | ||
seat=ticket.get("seat"), | ||
movie_session_id=ticket.get("movie_session"), | ||
order=order | ||
) | ||
|
||
return order | ||
|
||
|
||
def get_orders(username: str = None) -> QuerySet[Order]: | ||
if username: | ||
user = get_user_model().objects.get(username=username) | ||
return Order.objects.filter(user=user) | ||
|
||
return Order.objects.all() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from typing import Optional | ||
|
||
User = get_user_model() | ||
|
||
|
||
def create_user( | ||
username: str, | ||
password: str, | ||
email: Optional[str] = None, | ||
first_name: Optional[str] = None, | ||
last_name: Optional[str] = None | ||
) -> User: | ||
user = User.objects.create_user( | ||
username=username, | ||
password=password | ||
) | ||
if email: | ||
user.email = email | ||
if first_name: | ||
user.first_name = first_name | ||
if last_name: | ||
user.last_name = last_name | ||
user.save() | ||
return user | ||
Comment on lines
+15
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
|
||
def get_user(user_id: int) -> Optional[User]: | ||
try: | ||
return User.objects.get(id=user_id) | ||
except User.DoesNotExist: | ||
return None | ||
|
||
|
||
def update_user( | ||
user_id: int, | ||
username: Optional[str] = None, | ||
password: Optional[str] = None, | ||
email: Optional[str] = None, | ||
first_name: Optional[str] = None, | ||
last_name: Optional[str] = None | ||
) -> Optional[User]: | ||
try: | ||
user = User.objects.get(id=user_id) | ||
if username: | ||
Comment on lines
+45
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
user.username = username | ||
if password: | ||
user.set_password(password) | ||
if email: | ||
user.email = email | ||
if first_name: | ||
user.first_name = first_name | ||
if last_name: | ||
user.last_name = last_name | ||
user.save() | ||
return user | ||
except ObjectDoesNotExist: | ||
return None |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,8 @@ | |
|
||
INSTALLED_APPS = [ | ||
"db", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"django.contrib.auth", | ||
"django.contrib.contenttypes", | ||
] | ||
|
||
AUTH_USER_MODEL = "db.User" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read documentation. Import
settings
formdjango.conf
Link