-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add team views, add team demo data (#4)
* Add team views, add team demo data * Fix lint * Remove leftover script imports * Fix more lint --------- Co-authored-by: Jay Qi <[email protected]>
- Loading branch information
Showing
19 changed files
with
360 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import random | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.db import transaction | ||
|
||
from huntsite.puzzles import factories as puzzle_factories | ||
from huntsite.puzzles import services as puzzle_services | ||
from huntsite.teams import factories as team_factories | ||
|
||
PUZZLE_STARTED_PROP = 0.4 | ||
PUZZLE_SOLVED_PROP = 0.75 | ||
INCORRECTED_GUESS_MAX = 10 | ||
|
||
|
||
class Command(BaseCommand): | ||
def handle(self, *args, **options): | ||
with transaction.atomic(): | ||
# Puzzles | ||
puzzles = [] | ||
for i in range(24): | ||
puzzles.append(puzzle_factories.PuzzleFactory(calendar_entry__day=i + 1)) | ||
|
||
# Teams | ||
users = [] | ||
for i in range(10): | ||
users.append( | ||
team_factories.UserFactory( | ||
username=f"user{i}", password="hohohomerrychristmas!" | ||
) | ||
) | ||
|
||
# Make guesses | ||
for puzzle in puzzles: | ||
for user in users: | ||
if random.random() < PUZZLE_STARTED_PROP: | ||
# Incorrect guesses | ||
for _ in range(random.randint(0, INCORRECTED_GUESS_MAX)): | ||
puzzle_services.guess_submit( | ||
puzzle=puzzle, | ||
user=user, | ||
guess_text=puzzle_factories.answer_text_factory(), | ||
) | ||
if random.random() < PUZZLE_SOLVED_PROP: | ||
puzzle_services.guess_submit( | ||
puzzle=puzzle, user=user, guess_text=puzzle.answer | ||
) | ||
|
||
self.stdout.write(self.style.SUCCESS("create_demo_data complete.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,46 @@ | ||
import random | ||
|
||
from django.conf import settings | ||
from django.db.models.signals import post_save | ||
import factory | ||
from faker import Faker | ||
|
||
fake = Faker() | ||
|
||
|
||
def team_name_text_factory(instance=None) -> str: | ||
nb = random.randint(1, 3) | ||
return " ".join(fake.words(nb=nb)).title() | ||
|
||
|
||
def team_members_text_factory(instance=None) -> str: | ||
nb = random.randint(1, 3) | ||
return ", ".join(fake.first_name() for _ in range(nb)) | ||
|
||
|
||
@factory.django.mute_signals(post_save) | ||
class UserFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = settings.AUTH_USER_MODEL | ||
|
||
username = factory.Faker("user_name") | ||
email = factory.Faker("email") | ||
password = factory.Faker("password") | ||
|
||
team_name = factory.Faker("word") | ||
team_name = factory.lazy_attribute(team_name_text_factory) | ||
profile = factory.RelatedFactory("huntsite.teams.factories.TeamProfileFactory", "user") | ||
|
||
@factory.post_generation | ||
def password(obj, create, extracted, **kwargs): | ||
if extracted: | ||
obj.set_password(extracted) | ||
else: | ||
obj.set_unusable_password() | ||
|
||
|
||
@factory.django.mute_signals(post_save) | ||
class TeamProfileFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = "teams.Team" | ||
model = "teams.TeamProfile" | ||
|
||
user = factory.SubFactory(UserFactory) | ||
user = factory.SubFactory(UserFactory, profile=None) | ||
members = factory.lazy_attribute(team_members_text_factory) |
28 changes: 28 additions & 0 deletions
28
huntsite/teams/migrations/0002_teamprofile_members_alter_teamprofile_user_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 5.0.4 on 2024-07-20 14:23 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('teams', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='teamprofile', | ||
name='members', | ||
field=models.CharField(blank=True, help_text='List of team members.', max_length=255), | ||
), | ||
migrations.AlterField( | ||
model_name='teamprofile', | ||
name='user', | ||
field=models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='profile', to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.DeleteModel( | ||
name='TeamMember', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
from django.contrib.auth.decorators import login_required | ||
from django.template.response import TemplateResponse | ||
|
||
from huntsite.teams.forms import AccountManagementForm | ||
from huntsite.puzzles import selectors as puzzle_selectors | ||
from huntsite.teams import models | ||
|
||
|
||
@login_required | ||
def account_manage(request): | ||
"""View to manage the account of the user.""" | ||
user = request.user | ||
def team_detail(request, pk: int): | ||
"""View to display the team profile of the user.""" | ||
team = models.User.objects.select_related("profile").get(pk=pk) | ||
solves = puzzle_selectors.solve_list(team) | ||
context = { | ||
"user": user, | ||
"form": AccountManagementForm(instance=user), | ||
"user": request.user, | ||
"team": team, | ||
"solves": solves, | ||
"is_self": request.user == team, | ||
} | ||
return TemplateResponse(request, "account.html", context) | ||
return TemplateResponse(request, "team_detail.html", context) | ||
|
||
|
||
def team_list(request): | ||
"""View to display a list of all teams.""" | ||
teams = models.User.objects.select_related("profile").all() | ||
context = { | ||
"user": request.user, | ||
"teams": teams, | ||
} | ||
return TemplateResponse(request, "team_list.html", context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
from django.contrib.auth.decorators import login_required | ||
from django.template.response import TemplateResponse | ||
|
||
from huntsite.teams.forms import AccountManagementForm | ||
|
||
|
||
def home_page(request): | ||
return TemplateResponse(request, "home.html", {}) | ||
|
||
|
||
def about_page(request): | ||
return TemplateResponse(request, "about.html", {}) | ||
|
||
|
||
@login_required | ||
def account_manage(request): | ||
"""View to manage the account of the user.""" | ||
user = request.user | ||
context = { | ||
"user": user, | ||
"form": AccountManagementForm(instance=user), | ||
} | ||
return TemplateResponse(request, "account.html", context) |
Oops, something went wrong.