def edit_collection(request, collection): defaults = dict( title=collection.title, slug=collection.slug, description=collection.description) form = forms.EditCollectionForm( request.form, **defaults) if request.method == 'POST' and form.validate(): # Make sure there isn't already a Collection with such a slug # and userid. slug_used = check_collection_slug_used(request.db, collection.creator, request.form['slug'], collection.id) # Make sure there isn't already a Collection with this title existing_collection = request.db.Collection.find_one({ 'creator': request.user._id, 'title':request.form['title']}) if existing_collection and existing_collection.id != collection.id: messages.add_message( request, messages.ERROR, _('You already have a collection called "%s"!') % \ request.form['title']) elif slug_used: form.slug.errors.append( _(u'A collection with that slug already exists for this user.')) else: collection.title = unicode(request.form['title']) collection.description = unicode(request.form.get('description')) collection.slug = unicode(request.form['slug']) collection.save() return redirect(request, "mediagoblin.user_pages.user_collection", user=collection.get_creator.username, collection=collection.slug) if request.user.is_admin \ and collection.creator != request.user._id \ and request.method != 'POST': messages.add_message( request, messages.WARNING, _("You are editing another user's collection. Proceed with caution.")) return render_to_response( request, 'mediagoblin/edit/edit_collection.html', {'collection': collection, 'form': form})
def edit_collection(request, collection): defaults = dict( title=collection.title, slug=collection.slug, description=collection.description) form = forms.EditCollectionForm( request.form, **defaults) if request.method == 'POST' and form.validate(): # Make sure there isn't already a Collection with such a slug # and userid. slug_used = check_collection_slug_used(collection.creator, form.slug.data, collection.id) # Make sure there isn't already a Collection with this title existing_collection = request.db.Collection.query.filter_by( creator=request.user.id, title=form.title.data).first() if existing_collection and existing_collection.id != collection.id: messages.add_message( request, messages.ERROR, _('You already have a collection called "%s"!') % \ form.title.data) elif slug_used: form.slug.errors.append( _(u'A collection with that slug already exists for this user.')) else: collection.title = unicode(form.title.data) collection.description = unicode(form.description.data) collection.slug = unicode(form.slug.data) collection.save() return redirect_obj(request, collection) if request.user.has_privilege(u'admin') \ and collection.creator != request.user.id \ and request.method != 'POST': messages.add_message( request, messages.WARNING, _("You are editing another user's collection. Proceed with caution.")) return render_to_response( request, 'mediagoblin/edit/edit_collection.html', {'collection': collection, 'form': form})
def generate_slug(self): # import this here due to a cyclic import issue # (db.models -> db.mixin -> db.util -> db.models) from mediagoblin.db.util import check_collection_slug_used self.slug = slugify(self.title) duplicate = check_collection_slug_used(mg_globals.database, self.creator, self.slug, self.id) if duplicate: if self.id is not None: self.slug = u"%s-%s" % (self.id, self.slug) else: self.slug = None
def edit_collection(request, collection): defaults = dict( title=collection.title, slug=collection.slug, description=collection.description) form = forms.EditCollectionForm( request.form, **defaults) if request.method == 'POST' and form.validate(): # Make sure there isn't already a Collection with such a slug # and userid. slug_used = check_collection_slug_used(collection.creator, form.slug.data, collection.id) # Make sure there isn't already a Collection with this title existing_collection = request.db.Collection.query.filter_by( creator=request.user.id, title=form.title.data).first() if existing_collection and existing_collection.id != collection.id: messages.add_message( request, messages.ERROR, _('You already have a collection called "%s"!') % \ form.title.data) elif slug_used: form.slug.errors.append( _(u'A collection with that slug already exists for this user.')) else: collection.title = six.text_type(form.title.data) collection.description = six.text_type(form.description.data) collection.slug = six.text_type(form.slug.data) collection.save() return redirect_obj(request, collection) if request.user.has_privilege(u'admin') \ and collection.creator != request.user.id \ and request.method != 'POST': messages.add_message( request, messages.WARNING, _("You are editing another user's collection. Proceed with caution.")) return render_to_response( request, 'mediagoblin/edit/edit_collection.html', {'collection': collection, 'form': form})
def edit_collection(request, collection): defaults = dict(title=collection.title, slug=collection.slug, description=collection.description) form = forms.EditCollectionForm(request.form, **defaults) if request.method == "POST" and form.validate(): # Make sure there isn't already a Collection with such a slug # and userid. slug_used = check_collection_slug_used(collection.creator, form.slug.data, collection.id) # Make sure there isn't already a Collection with this title existing_collection = request.db.Collection.find_one({"creator": request.user.id, "title": form.title.data}) if existing_collection and existing_collection.id != collection.id: messages.add_message( request, messages.ERROR, _('You already have a collection called "%s"!') % form.title.data ) elif slug_used: form.slug.errors.append(_(u"A collection with that slug already exists for this user.")) else: collection.title = unicode(form.title.data) collection.description = unicode(form.description.data) collection.slug = unicode(form.slug.data) collection.save() return redirect_obj(request, collection) if request.user.is_admin and collection.creator != request.user.id and request.method != "POST": messages.add_message( request, messages.WARNING, _("You are editing another user's collection. Proceed with caution.") ) return render_to_response( request, "mediagoblin/edit/edit_collection.html", {"collection": collection, "form": form} )
def check_slug_used(self, slug): # import this here due to a cyclic import issue # (db.models -> db.mixin -> db.util -> db.models) from mediagoblin.db.util import check_collection_slug_used return check_collection_slug_used(self.creator, slug, self.id)
def check_slug_used(self, slug): # import this here due to a cyclic import issue # (db.models -> db.mixin -> db.util -> db.models) from mediagoblin.db.util import check_collection_slug_used return check_collection_slug_used(self.actor, slug, self.id)