|
10 | 10 |
|
11 | 11 |
|
12 | 12 | class HackerTeam(IsHackerMixin, TabsView): |
13 | | - template_name = 'team.html' |
| 13 | + """ |
| 14 | + View for hackers to manage their team. |
| 15 | + Hackers can create a new team, join an existing team or leave their current team, |
| 16 | + by making a POST request with theappropriate action. |
| 17 | + """ |
| 18 | + |
| 19 | + template_name = "team.html" |
14 | 20 |
|
15 | 21 | def get_current_tabs(self): |
16 | 22 | return hacker_tabs(self.request.user) |
17 | 23 |
|
18 | 24 | def get_context_data(self, **kwargs): |
19 | 25 | c = super(HackerTeam, self).get_context_data(**kwargs) |
20 | | - team = getattr(self.request.user, 'team', None) |
21 | | - app = getattr(self.request.user, 'hackerapplication_application', None) |
| 26 | + team = getattr(self.request.user, "team", None) |
| 27 | + app = getattr(self.request.user, "hackerapplication_application", None) |
22 | 28 | teammates = [] |
23 | 29 | if team: |
24 | | - teammates = models.Team.objects.filter(team_code=team.team_code) \ |
25 | | - .values('user__name', 'user__email', 'user__hackerapplication_application') |
26 | | - teammates = list(map(lambda x: |
27 | | - {'name': x['user__name'], 'email': x['user__email'], |
28 | | - 'app': x['user__hackerapplication_application']}, |
29 | | - teammates)) |
| 30 | + teammates = models.Team.objects.filter(team_code=team.team_code).values( |
| 31 | + "user__name", "user__email", "user__hackerapplication_application" |
| 32 | + ) |
| 33 | + teammates = list( |
| 34 | + map( |
| 35 | + lambda x: { |
| 36 | + "name": x["user__name"], |
| 37 | + "email": x["user__email"], |
| 38 | + "app": x["user__hackerapplication_application"], |
| 39 | + }, |
| 40 | + teammates, |
| 41 | + ) |
| 42 | + ) |
30 | 43 | instance = models.Team() |
31 | | - instance.team_code = '' |
| 44 | + instance.team_code = "" |
32 | 45 | form = forms.JoinTeamForm(instance=instance) |
33 | | - c.update({'team': team, 'teammates': teammates, 'app': app, 'form': form}) |
| 46 | + c.update({"team": team, "teammates": teammates, "app": app, "form": form}) |
34 | 47 | return c |
35 | 48 |
|
36 | 49 | def post(self, request, *args, **kwargs): |
37 | 50 |
|
38 | | - if request.POST.get('create', None): |
| 51 | + if request.POST.get("create", None): |
39 | 52 | team = models.Team() |
40 | 53 | team.user = request.user |
41 | 54 | team.save() |
42 | | - return HttpResponseRedirect(reverse('teams')) |
43 | | - if request.POST.get('leave', None): |
44 | | - team = getattr(request.user, 'team', None) |
| 55 | + return HttpResponseRedirect(reverse("teams")) |
| 56 | + if request.POST.get("leave", None): |
| 57 | + team = getattr(request.user, "team", None) |
45 | 58 | if team: |
46 | 59 | team.delete() |
47 | | - return HttpResponseRedirect(reverse('teams')) |
| 60 | + return HttpResponseRedirect(reverse("teams")) |
48 | 61 | else: |
49 | 62 | form = forms.JoinTeamForm(request.POST, request.FILES) |
50 | 63 | if form.is_valid(): |
51 | 64 | team = form.save(commit=False) |
52 | 65 | team.user = request.user |
53 | 66 | team.save() |
54 | 67 |
|
55 | | - messages.success(request, 'Team joined successfully!') |
| 68 | + messages.success(request, "Team joined successfully!") |
56 | 69 |
|
57 | | - return HttpResponseRedirect(reverse('teams')) |
| 70 | + return HttpResponseRedirect(reverse("teams")) |
58 | 71 | else: |
59 | 72 | c = self.get_context_data() |
60 | | - c.update({'form': form}) |
| 73 | + c.update({"form": form}) |
61 | 74 | return render(request, self.template_name, c) |
0 commit comments