diff --git a/events/migrations/0032_add_team_slug.py b/events/migrations/0032_add_team_slug.py new file mode 100644 index 0000000..9ce9888 --- /dev/null +++ b/events/migrations/0032_add_team_slug.py @@ -0,0 +1,34 @@ +# Generated by Django 2.0 on 2018-06-09 01:44 + +from django.db import migrations, models + +from events.utils import slugify + + +def add_team_slug(apps, schema_editor): + Team = apps.get_model('events', 'Team') + for team in Team.objects.all(): + team.slug = slugify(team.name) + team.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('events', '0031_add_sponsors'), + ] + + operations = [ + migrations.AddField( + model_name='team', + name='slug', + field=models.CharField(max_length=256, null=True), + ), + migrations.RunPython(add_team_slug, reverse_code=migrations.RunPython.noop), + migrations.AlterField( + model_name='team', + name='slug', + field=models.CharField(default='', max_length=256), + preserve_default=False, + ), + ] diff --git a/events/models/profiles.py b/events/models/profiles.py index 9cdefdc..674b792 100644 --- a/events/models/profiles.py +++ b/events/models/profiles.py @@ -222,6 +222,7 @@ class SponsorSerializer(serializers.ModelSerializer): class Team(models.Model): name = models.CharField(_("Team Name"), max_length=256, null=False, blank=False) + slug = models.CharField(max_length=256, null=False, blank=False, unique=True) organization = models.ForeignKey(Organization, related_name='teams', null=True, blank=True, on_delete=models.CASCADE) description = models.TextField(blank=True, null=True) @@ -282,6 +283,12 @@ class Team(models.Model): if self.city is not None: self.spr = self.city.spr self.country = self.spr.country + new_slug = slugify(self.name) + slug_matches = list(Team.objects.filter(slug=new_slug)) + if len(slug_matches) == 0 or (len(slug_matches) == 1 and slug_matches[0].id == self.id): + self.slug = new_slug + else: + self.slug = '%s-%s' % (new_slug, self.id) super().save(*args, **kwargs) # Call the "real" save() method. diff --git a/get_together/templates/get_together/events/create_event_team_select.html b/get_together/templates/get_together/events/create_event_team_select.html index 15c5876..2b62654 100644 --- a/get_together/templates/get_together/events/create_event_team_select.html +++ b/get_together/templates/get_together/events/create_event_team_select.html @@ -14,7 +14,7 @@
- + {% if team.category %} {{team.name}} {% else %} diff --git a/get_together/templates/get_together/events/show_event.html b/get_together/templates/get_together/events/show_event.html index 8353f01..7a8a0a4 100644 --- a/get_together/templates/get_together/events/show_event.html +++ b/get_together/templates/get_together/events/show_event.html @@ -101,7 +101,7 @@

{{ event.name }}

-

Hosted by {{ team.name }}

+

Hosted by {{ team.name }}

{% if settings.SOCIAL_AUTH_TWITTER_KEY %} Tweet {% endif %} diff --git a/get_together/templates/get_together/events/show_series.html b/get_together/templates/get_together/events/show_series.html index 69c3f5a..ad97b96 100644 --- a/get_together/templates/get_together/events/show_series.html +++ b/get_together/templates/get_together/events/show_series.html @@ -39,7 +39,7 @@ Edit Series {% endif %} -

Hosted by {{ team.name }}

+

Hosted by {{ team.name }}


{{ series.summary|markdown }}

diff --git a/get_together/templates/get_together/index.html b/get_together/templates/get_together/index.html index 147ec69..6d20e6e 100644 --- a/get_together/templates/get_together/index.html +++ b/get_together/templates/get_together/index.html @@ -68,7 +68,7 @@
-

{{team.city}}

+

{{team.city}}

{{ team.members.count }} members
- View + View
diff --git a/get_together/templates/get_together/orgs/create_common_event_team_select.html b/get_together/templates/get_together/orgs/create_common_event_team_select.html index 6cfda3d..315730f 100644 --- a/get_together/templates/get_together/orgs/create_common_event_team_select.html +++ b/get_together/templates/get_together/orgs/create_common_event_team_select.html @@ -14,7 +14,7 @@
- + {% if team.category %} {{team.name}} {% else %} diff --git a/get_together/templates/get_together/orgs/show_org.html b/get_together/templates/get_together/orgs/show_org.html index 3b71437..61c9441 100644 --- a/get_together/templates/get_together/orgs/show_org.html +++ b/get_together/templates/get_together/orgs/show_org.html @@ -65,7 +65,7 @@ diff --git a/get_together/templates/get_together/teams/invite_members.html b/get_together/templates/get_together/teams/invite_members.html index 346810a..f91a8f8 100644 --- a/get_together/templates/get_together/teams/invite_members.html +++ b/get_together/templates/get_together/teams/invite_members.html @@ -7,7 +7,7 @@
-

Invite people to {{ team.name }} +

Invite people to {{ team.name }}

Add a list of emails, separated by commas

diff --git a/get_together/templates/get_together/teams/list_teams.html b/get_together/templates/get_together/teams/list_teams.html index 086eae3..490b278 100644 --- a/get_together/templates/get_together/teams/list_teams.html +++ b/get_together/templates/get_together/teams/list_teams.html @@ -20,19 +20,21 @@
-

{{team.city}}

+

{{team.city}}

{{ team.members.count }} members
- View + View
diff --git a/get_together/templates/get_together/teams/manage_members.html b/get_together/templates/get_together/teams/manage_members.html index 2f440fa..588ce0e 100644 --- a/get_together/templates/get_together/teams/manage_members.html +++ b/get_together/templates/get_together/teams/manage_members.html @@ -21,7 +21,7 @@
-

Members of {{ team.name }} +

Members of {{ team.name }}

Invite Members

diff --git a/get_together/templates/get_together/users/show_profile.html b/get_together/templates/get_together/users/show_profile.html index 1e883c6..73a4bd5 100644 --- a/get_together/templates/get_together/users/show_profile.html +++ b/get_together/templates/get_together/users/show_profile.html @@ -61,7 +61,7 @@ diff --git a/get_together/urls.py b/get_together/urls.py index 009950a..bfec869 100644 --- a/get_together/urls.py +++ b/get_together/urls.py @@ -64,7 +64,7 @@ urlpatterns = [ path('events/all/', views.events_list_all, name='all-events'), path('teams/', views.teams_list, name='teams'), path('teams/all/', views.teams_list_all, name='all-teams'), - path('team//', views.show_team, name='show-team'), + path('team//', views.show_team_by_id, name='show-team'), path('team//+edit/', views.edit_team, name='edit-team'), path('team//+join/', event_views.join_team, name='join-team'), path('team//+leave/', event_views.leave_team, name='leave-team'), @@ -109,6 +109,8 @@ urlpatterns = [ path('about/', include('django.contrib.flatpages.urls')), path('oauth/', include('social_django.urls', namespace='social')), + + path('/', views.show_team_by_slug, name='show-team-by-slug'), ] if settings.DEBUG: urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/get_together/views/events.py b/get_together/views/events.py index 2d205f8..849a48d 100644 --- a/get_together/views/events.py +++ b/get_together/views/events.py @@ -114,7 +114,7 @@ def create_event(request, team_id): team = get_object_or_404(Team, id=team_id) if not request.user.profile.can_create_event(team): messages.add_message(request, messages.WARNING, message=_('You can not create events for this team.')) - return redirect('show-team', team_id=team.pk) + return redirect('show-team-by-slug', team_slug=team.slug) new_event = Event(team=team, created_by=request.user.profile) @@ -669,10 +669,10 @@ def delete_event(request, event_id): elif request.method == 'POST': form = DeleteEventForm(request.POST) if form.is_valid() and form.cleaned_data['confirm']: - team_id = event.team_id - delete_event_searchable(event); + team_slug = event.team.slug + delete_event_searchable(event) event.delete() - return redirect('show-team', team_id) + return redirect('show-team-by-slug', team_slug) else: context = { 'team': event.team, @@ -734,9 +734,9 @@ def delete_series(request, series_id): elif request.method == 'POST': form = DeleteEventSeriesForm(request.POST) if form.is_valid() and form.cleaned_data['confirm']: - team_id = series.team_id + team_slug = series.team.slug series.delete() - return redirect('show-team', team_id) + return redirect('show-team-by-slug', team_slug) else: context = { 'team': series.team, diff --git a/get_together/views/teams.py b/get_together/views/teams.py index e645ab9..23e0228 100644 --- a/get_together/views/teams.py +++ b/get_together/views/teams.py @@ -15,6 +15,7 @@ from events.models.profiles import Organization, Team, UserProfile, Member from events.models.events import Event, CommonEvent, Place, Attendee from events.forms import TeamForm, NewTeamForm, DeleteTeamForm, TeamContactForm, TeamInviteForm from events import location +from events.utils import slugify from accounts.models import EmailRecord @@ -43,8 +44,18 @@ def teams_list_all(request, *args, **kwargs): } return render(request, 'get_together/teams/list_teams.html', context) -def show_team(request, team_id, *args, **kwargs): + +def show_team_by_slug(request, team_slug): + team = get_object_or_404(Team, slug=team_slug) + return show_team(request, team) + + +def show_team_by_id(request, team_id): team = get_object_or_404(Team, id=team_id) + return redirect('show-team-by-slug', team_slug=team.slug) + + +def show_team(request, team): upcoming_events = Event.objects.filter(team=team, end_time__gt=datetime.datetime.now()).order_by('start_time') recent_events = Event.objects.filter(team=team, end_time__lte=datetime.datetime.now()).order_by('-start_time')[:5] context = { @@ -58,6 +69,7 @@ def show_team(request, team_id, *args, **kwargs): } return render(request, 'get_together/teams/show_team.html', context) + @login_required def create_team(request, *args, **kwargs): if request.method == 'GET': @@ -74,7 +86,7 @@ def create_team(request, *args, **kwargs): new_team.owner_profile = request.user.profile new_team.save() Member.objects.create(team=new_team, user=request.user.profile, role=Member.ADMIN) - return redirect('show-team', team_id=new_team.pk) + return redirect('show-team-by-slug', team_slug=new_team.slug) else: context = { 'team_form': form, @@ -88,7 +100,7 @@ def edit_team(request, team_id): team = get_object_or_404(Team, id=team_id) if not request.user.profile.can_edit_team(team): messages.add_message(request, messages.WARNING, message=_('You can not make changes to this team.')) - return redirect('show-team', team_id=team.pk) + return redirect('show-team-by-slug', team_slug=team.slug) if request.method == 'GET': form = TeamForm(instance=team) @@ -104,7 +116,7 @@ def edit_team(request, team_id): new_team = form.save() new_team.owner_profile = request.user.profile new_team.save() - return redirect('show-team', team_id=new_team.pk) + return redirect('show-team-by-slug', team_slug=new_team.slug) else: context = { 'team': team, @@ -119,7 +131,7 @@ def delete_team(request, team_id): team = get_object_or_404(Team, id=team_id) if not request.user.profile.can_edit_team(team): messages.add_message(request, messages.WARNING, message=_('You can not make changes to this team.')) - return redirect('show-team', team_id) + return redirect('show-team-by-slug', team.slug) if request.method == 'GET': form = DeleteTeamForm() @@ -149,7 +161,7 @@ def manage_members(request, team_id): team = get_object_or_404(Team, id=team_id) if not request.user.profile.can_edit_team(team): messages.add_message(request, messages.WARNING, message=_('You can not manage this team\'s members.')) - return redirect('show-team', team_id) + return redirect('show-team-by-slug', team.slug) members = Member.objects.filter(team=team).order_by('user__realname') member_choices = [(member.id, member.user) for member in members if member.user.user.account.is_email_confirmed] @@ -162,7 +174,7 @@ def manage_members(request, team_id): body = contact_form.cleaned_data['body'] if to is not 'admins' and not request.user.profile.can_edit_team(team): messages.add_message(request, messages.WARNING, message=_('You can not contact this team\'s members.')) - return redirect('show-team', team_id) + return redirect('show-team-by-slug', team.slug) if to == 'all': count = 0 for member in Member.objects.filter(team=team):