Permalink
Browse files

Revert "Improve group URLs r=timw"

This reverts commit 2670672.
  • Loading branch information...
1 parent 7745254 commit c7f4b5822111d47b817e6179da787a107345f5e0 @tallowen tallowen committed Apr 27, 2012
@@ -1,128 +0,0 @@
-# encoding: utf-8
-from south.db import db
-from south.v2 import SchemaMigration
-
-from groups.models import Group
-from users.models import UserProfile
-
-
-class Migration(SchemaMigration):
-
- def forwards(self, orm):
- if not db.dry_run:
- all_profiles = UserProfile.objects.all()
- for group in orm.Group.objects.extra(where=[('`url` IN ( '
- 'SELECT `url` FROM `group` GROUP BY `url` HAVING ('
- 'COUNT(`url`) > 1))')]):
- groups = orm.Group.objects.filter(url=group.url)
- group_ids = [g.id for g in groups]
-
- # South's access to ORM models is spotty and I'd rather this
- # be slow/inefficient than non-abstracted. I'm aware this could
- # be _greatly_ optimized, but I don't care: it only runs once.
- profile_ids = [p.id for p in all_profiles
- if p.groups.filter(id__in=group_ids)]
-
- # We're going to use the "first" group with a similar URL, so
- # make sure that all users who had any "related-by-URL" groups
- # now belong to this group.
- for p in UserProfile.objects.filter(id__in=profile_ids):
- p.groups.add(Group.objects.get(id=group_ids[0]))
-
- # Cascading delete removes group membership in
- # UserProfile objects.
- for g in Group.objects.filter(id__in=group_ids[1:]):
- g.delete()
-
- # Adding unique constraint on 'Group', fields ['url']
- db.create_unique('group', ['url'])
-
- # Rename url to slug
- db.rename_column('group', 'url', 'slug')
-
-
- def backwards(self, orm):
-
- # Reverse renaming.
- db.rename_column('group', 'slug', 'url')
-
- # Removing unique constraint on 'Group', fields ['url']
- db.delete_unique('group', ['url'])
-
-
- models = {
- 'auth.group': {
- 'Meta': {'object_name': 'Group'},
- 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
- 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
- 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
- },
- 'auth.permission': {
- 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
- 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
- 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
- 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
- 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
- },
- 'auth.user': {
- 'Meta': {'object_name': 'User'},
- 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
- 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
- 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
- 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
- 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
- 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
- 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
- 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
- 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
- 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
- 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
- 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
- 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
- },
- 'contenttypes.contenttype': {
- 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
- 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
- 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
- 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
- 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
- },
- 'groups.group': {
- 'Meta': {'object_name': 'Group', 'db_table': "'group'"},
- 'always_auto_complete': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
- 'auto_complete': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
- 'description': ('django.db.models.fields.TextField', [], {'default': "''", 'max_length': '255', 'blank': 'True'}),
- 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
- 'irc_channel': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '63', 'blank': 'True'}),
- 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '50', 'db_index': 'True'}),
- 'steward': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['users.UserProfile']", 'null': 'True', 'blank': 'True'}),
- 'system': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
- 'url': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '50', 'db_index': 'True'}),
- 'website': ('django.db.models.fields.URLField', [], {'default': "''", 'max_length': '200', 'blank': 'True'}),
- 'wiki': ('django.db.models.fields.URLField', [], {'default': "''", 'max_length': '200', 'blank': 'True'})
- },
- 'groups.skill': {
- 'Meta': {'object_name': 'Skill'},
- 'always_auto_complete': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
- 'auto_complete': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
- 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
- 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '50', 'db_index': 'True'})
- },
- 'users.userprofile': {
- 'Meta': {'object_name': 'UserProfile', 'db_table': "'profile'"},
- 'bio': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
- 'display_name': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255', 'blank': 'True'}),
- 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['groups.Group']", 'symmetrical': 'False'}),
- 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
- 'ircname': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '63', 'blank': 'True'}),
- 'is_vouched': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
- 'last_updated': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'auto_now': 'True', 'blank': 'True'}),
- 'photo': ('sorl.thumbnail.fields.ImageField', [], {'default': "''", 'max_length': '100', 'blank': 'True'}),
- 'skills': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['groups.Skill']", 'symmetrical': 'False'}),
- 'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'}),
- 'vouched_by': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': "orm['users.UserProfile']", 'null': 'True'}),
- 'website': ('django.db.models.fields.URLField', [], {'default': "''", 'max_length': '200', 'null': 'True', 'blank': 'True'})
- }
- }
-
- complete_apps = ['groups']
View
@@ -40,7 +40,7 @@ def __unicode__(self):
class Group(GroupBase):
- slug = models.SlugField(unique=True)
+ url = models.SlugField()
system = models.BooleanField(db_index=True, default=False)
# Has a steward taken ownership of this group?
description = models.TextField(max_length=255,
@@ -71,7 +71,7 @@ class Skill(GroupBase):
def _create_url_slug(sender, instance, raw, using, **kwargs):
"""Create a Group's URL slug when it's first saved."""
if not instance.pk:
- instance.slug = slugify(instance.name)
+ instance.url = slugify(instance.name.lower())
@receiver(models.signals.pre_save, sender=Skill)
@@ -22,7 +22,7 @@ <h1 class="span12">Users in Group "{{ name }}"</h1>
<div class="span12">
<p class="span6">{{ group.description }}</p>
<form class="curated-toggle span2 offset3"
- action="{{ url('group_toggle', group.slug) }}"
+ action="{{ url('group_toggle', group.id, group.url) }}"
id="toggle-group" method="post">
{{ csrf() }}
@@ -95,7 +95,7 @@ <h1 class="span12">Users in Group "{{ name }}"</h1>
</ul>
</div>
{% elif not group.system %}
- <form action="{{ url('group_toggle', group.slug) }}"
+ <form action="{{ url('group_toggle', group.id, group.url) }}"
class="span2" id="toggle-group" method="post">
{{ csrf() }}
@@ -20,7 +20,7 @@
{% for group in groups %}
<li class="tagit-choice ui-widget-content ui-state-default
ui-corner-all">
- <a href="{{ url('group', group.slug) }}">
+ <a href="{{ url('group', group.id, group.url) }}">
{{ group.name }}
</a>
</li>
@@ -18,7 +18,6 @@ def setUp(self):
super(GroupTest, self).setUp()
self.NORMAL_GROUP = Group.objects.create(name='cheesezilla')
self.SYSTEM_GROUP = Group.objects.create(name='ghost', system=True)
- Group.objects.create(name='daft', system=True)
def test_default_groups(self):
"""Ensure the user has the proper amount of groups upon creation."""
@@ -184,7 +183,7 @@ def test_toggle_membership_on_group_page(self):
self.client.login(email=self.mozillian.email)
response = self.client.get(reverse('group', args=[
- self.NORMAL_GROUP.slug]))
+ self.NORMAL_GROUP.id, self.NORMAL_GROUP.url]))
doc = pq(response.content)
assert not profile.groups.filter(id=self.NORMAL_GROUP.id), (
@@ -212,8 +211,8 @@ def test_toggle_membership_on_group_page(self):
# Test against a system group, where we shouldn't be able to toggle
# membership.
- response = self.client.get(reverse('group',
- args=[self.SYSTEM_GROUP.slug]))
+ response = self.client.get(reverse('group', args=[self.SYSTEM_GROUP.id,
+ self.SYSTEM_GROUP.url]))
doc = pq(response.content)
assert not profile.groups.filter(id=self.SYSTEM_GROUP.id), (
@@ -223,8 +222,8 @@ def test_toggle_membership_on_group_page(self):
'"Join Group" button should not be present in the response.')
# Attempt to manually toggle the group membership
- r = self.client.post(reverse('group_toggle',
- args=[self.SYSTEM_GROUP.slug]), follow=True)
+ r = self.client.post(reverse('group_toggle', args=[
+ self.SYSTEM_GROUP.id, self.SYSTEM_GROUP.url]), follow=True)
assert not profile.groups.filter(id=self.SYSTEM_GROUP.id), (
'User should not be in the "%s" group' %
self.SYSTEM_GROUP.name)
View
@@ -8,9 +8,9 @@
urlpatterns = patterns('',
url('^groups$', views.index, name='group_index'),
+ url('^group/(?P<id>\d+)-(?P<url>[^/]+)$', views.show, name='group'),
+ url('^group/(?P<id>\d+)-(?P<url>[^/]+)/toggle$', views.toggle,
+ name='group_toggle'),
url('^groups/search$', views.search, dict(searched_object=Group), name='group_search'),
url('^skills/search$', views.search, dict(searched_object=Skill), name='skill_search'),
- url('^groups/(?P<slug>[\w-]{1,50})$', views.show, name='group'),
- url('^groups/(?P<slug>[\w-]{1,50})/toggle$',
- views.toggle, name='group_toggle'),
)
View
@@ -54,9 +54,11 @@ def search(request, searched_object=Group):
@vouch_required
-def show(request, slug):
- """List all users with this group."""
- group = get_object_or_404(Group, slug=slug)
+def show(request, id, url=None):
+ """ List all users with this group."""
+ group = get_object_or_404(Group, id=id)
+ if not url:
+ redirect(reverse('group', args=[group.id, group.url]))
form = forms.SearchForm(request.GET)
limit = forms.PAGINATION_LIMIT
if form.is_valid():
@@ -107,9 +109,9 @@ def show(request, slug):
@require_POST
@vouch_required
-def toggle(request, slug):
+def toggle(request, id, url):
"""Toggle the current user's membership of a group."""
- group = get_object_or_404(Group, slug=slug)
+ group = get_object_or_404(Group, url=url)
profile = request.user.get_profile()
# We don't operate on system groups using this view.
@@ -119,4 +121,4 @@ def toggle(request, slug):
else:
profile.groups.add(group)
- return redirect(reverse('group', args=[group.slug]))
+ return redirect(reverse('group', args=[group.id, group.url]))
View
@@ -5,7 +5,6 @@
from django.conf import settings
from django.core.urlresolvers import resolve
from django.utils.safestring import mark_safe
-from django.template.defaultfilters import slugify
import happyforms
from tower import ugettext as _, ugettext_lazy as _lazy
@@ -149,33 +148,23 @@ def clean_groups(self):
'alphanumeric characters, dashes, '
'spaces.'))
- system_groups = list(self.instance.groups.filter(system=True))
+ system_groups = [g.name for g in self.instance.groups.all()
+ if g.system]
- groups = [g.strip()
- for g in self.cleaned_data['groups'].lower().split(',')
- if g and ',' not in g]
+ new_groups = [g.strip()
+ for g in self.cleaned_data['groups'].lower().split(',')
+ if g and ',' not in g]
- def handle_group(name):
- existing_group = Group.objects.filter(slug=slugify(name))[:1]
-
- if not existing_group:
- return Group.objects.create(name=name)
- return existing_group[0]
-
- return system_groups + map(handle_group, groups)
+ return system_groups + new_groups
def clean_skills(self):
if not re.match(r'^[a-zA-Z0-9 .:,-]*$', self.cleaned_data['skills']):
raise forms.ValidationError(_(u'Skills can only contain '
'alphanumeric characters, dashes, '
'spaces.'))
-
- # Get the name of the skills we need to find.
- skills_names = [s for s in
- self.cleaned_data['skills'].lower().split(',') if s]
-
- get_or_create = lambda n: Skill.objects.get_or_create(name=n)[0]
- return map(get_or_create, skills_names)
+ return [s.strip()
+ for s in self.cleaned_data['skills'].lower().split(',')
+ if s and ',' not in s]
def save(self, request):
"""Save the data to profile."""
@@ -133,7 +133,7 @@ <h2 class="fn p-name">
<li class="tagit-choice ui-widget-content ui-state-default
ui-corner-all p-category category">
{% if request.user.get_profile().is_vouched %}
- <a href="{{ url('group', group.slug) }}">
+ <a href="{{ url('group', group.id, group.url) }}">
{{ group.name }}
</a>
{% else %}
View
@@ -92,12 +92,19 @@ def set_membership(self, model, membership_list):
m2mfield = self.skills
# Remove any non-system groups that weren't supplied in this list.
- m2mfield.remove(*[m for m in m2mfield.all()
- if m not in membership_list
- and not getattr(m, 'system', False)])
+ m2mfield.remove(*[g for g in m2mfield.all()
+ if g.name not in membership_list
+ and not getattr(g, 'system', False)])
- m2mfield.add(*[m for m in membership_list if not
- getattr(m, 'system', False)])
+ # Add/create the rest of the groups
+ groups_to_add = []
+ for g in membership_list:
+ (group, created) = model.objects.get_or_create(name=g)
+
+ if not getattr(g, 'system', False):
+ groups_to_add.append(group)
+
+ m2mfield.add(*groups_to_add)
def is_complete(self):
"""

0 comments on commit c7f4b58

Please sign in to comment.