1# Copyright 2013-2018 The Distro Tracker Developers
2# See the COPYRIGHT file at the top-level directory of this distribution and
3# at https://deb.li/DTAuthors
4#
5# This file is part of Distro Tracker. It is subject to the license terms
6# in the LICENSE file found in the top-level directory of this
7# distribution and at https://deb.li/DTLicense. No part of Distro Tracker,
8# including this file, may be copied, modified, propagated, or distributed
9# except according to the terms contained in the LICENSE file.
10"""Forms for the :mod:`distro_tracker.core` app."""
11from django import forms
12from django.conf import settings
13from django.template.defaultfilters import slugify
15from distro_tracker.accounts.models import UserEmail
16from distro_tracker.core.models import SourcePackageName, Team
19class CreateTeamForm(forms.ModelForm):
20 maintainer_email = forms.EmailField(
21 required=False,
22 help_text='All packages having this email address listed in the '
23 'Maintainer or Uploaders fields will be automatically added '
24 'to the team.'
25 )
27 class Meta:
28 model = Team
29 fields = (
30 'name',
31 'slug',
32 'public',
33 'description',
34 'url',
35 )
36 labels = {
37 'public': 'Visible in the list of teams and free to join by anyone',
38 }
39 help_texts = {
40 'slug': 'Used in the URL (/teams/<em>identifier</em>/) and in the '
41 'associated email address '
42 'team+<em>identifier</em>@{}.'.format(
43 settings.DISTRO_TRACKER_FQDN),
44 }
46 def __init__(self, *args, **kwargs):
47 super(CreateTeamForm, self).__init__(*args, **kwargs)
48 self.fields['slug'].required = self.is_update()
50 def is_update(self):
51 return hasattr(self, 'instance') and self.instance.pk is not None
53 def clean(self):
54 """
55 Provides a default value for the slug field based on the given team
56 name, but only if the team is only just being created (not an
57 update of an existing instance).
58 """
59 cleaned_data = super(CreateTeamForm, self).clean()
60 if not self.is_update():
61 if not cleaned_data['slug'] and 'name' in cleaned_data: 61 ↛ 62line 61 didn't jump to line 62, because the condition on line 61 was never true
62 cleaned_data['slug'] = slugify(cleaned_data['name'])
64 return cleaned_data
66 def save(self, *args, **kwargs):
67 # Create a maintainer email instance based on the email given to the
68 # form.
69 if 'maintainer_email' in self.cleaned_data: 69 ↛ 79line 69 didn't jump to line 79, because the condition on line 69 was never false
70 if self.cleaned_data['maintainer_email']:
71 maintainer_email, _ = UserEmail.objects.get_or_create(
72 email=self.cleaned_data['maintainer_email'])
73 self.instance.maintainer_email = maintainer_email
74 else:
75 self.instance.maintainer_email = None
77 # The instance needs to be saved before many-to-many relations can
78 # reference it.
79 instance = super(CreateTeamForm, self).save(commit=True)
80 # If the maintainer email is set, associate all packages maintained
81 # by that email to the team.
82 if not instance.maintainer_email:
83 return instance
85 filter_kwargs = {
86 'source_package_versions__maintainer__contributor_email__email': (
87 instance.maintainer_email
88 )
89 }
90 packages = SourcePackageName.objects.filter(**filter_kwargs)
91 packages = packages.distinct()
93 # Add all the packages to the team's set
94 instance.packages.add(*packages)
96 return instance
99class AddTeamMemberForm(forms.Form):
100 email = forms.EmailField()