def _generate_slug(self, instance, value): count = 1 slug = slug_attempt = slugify(value) cls = instance.__class__ while cls.objects(**{self.db_field: slug_attempt}).count() > 0: slug_attempt = '%s-%s' % (slug, count) count += 1 return slug_attempt
def create(request): form = NewAccountForm(request.POST or None) if form.is_valid(): # create the account instance new_account = Account() new_account.name = request.POST['company_name'] new_account.phone = request.POST['phone'] new_account.address1 = request.POST['address1'] new_account.address2 = request.POST['address2'] new_account.city = request.POST['city'] new_account.state = request.POST['state'] new_account.postal_zip = request.POST['postal_zip'] new_account.country = request.POST['country'] new_account.save() messages.success(request, new_account) # create new user and save profile details; save new user username = request.POST['username'] email = request.POST['email'] password = request.POST['password2'] new_user = User.create_user(username=username, email=email, password=password) new_user.first_name = request.POST['first_name'] new_user.last_name = request.POST['last_name'] new_user.save() # set new user as admin for account instance; save account new_account.admin = new_user new_account.save() # authenticate and log in user auth_user = authenticate(username=username, password=password) login(request=request, user=auth_user) messages.success(request, 'Successfully logged in as %s' % \ auth_user.username) slug = slugify(new_account.name) return HttpResponseRedirect('/%s/welcome/' % slug) data = {'title': 'Kolabria - Create a new Account ', 'form': form, } return render_to_response('account/create.html', data, context_instance=RequestContext(request))
def _generate_slug(self, instance, value): """Query the database for similarly matching values. Then increment the maximum trailing integer. In the future this will rely on map-reduce(?). This method first makes a basic slug from the given value. Then it checks to see if any documents in the database share that same value in the same field. If it finds matching results then it will attempt to increment the counter on the end of the slug. It uses pymongo directly because mongoengine's own querysets rely on each field's __set__ method, which results in endless recrusion. Good times. """ collection = instance.__class__.objects._collection slug = slugify(value) slug_regex = '^%s' % slug existing_docs = [ {'id': doc['_id'], self.db_field: doc[self.db_field]} for doc in collection.find({self.db_field: {'$regex':slug_regex}}) ] matches = [int(re.search(r'-[\d]+$', doc[self.db_field]).group()[-1:]) for doc in existing_docs if re.search(r'-[\d]+$', doc[self.db_field])] # Four scenarios: # (1) No match is found, this is a brand new slug # (2) A matching document is found, but it's this one # (3) A matching document is found but without any number # (4) A matching document is found with an incrementing value next = 1 if len(existing_docs) == 0: return slug elif instance.id in [doc['id'] for doc in existing_docs]: return slug elif not matches: return u'%s-%s' % (slug, next) else: next = max(matches) + 1 return u'%s-%s' % (slug, next)
def test_slugify(text, slug): assert slugify(text) == slug
def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(self.name) super(ChatRoom, self).save(*args, **kwargs)