def new_gallery(request): """Creates a new gallery.""" if request.method == "POST": form = GalleryForm(request.POST) if form.is_valid(): data = form.data # Creating the gallery gal = Gallery() gal.title = data["title"] gal.subtitle = data["subtitle"] gal.slug = slugify(data["title"]) gal.pubdate = datetime.now() gal.save() # Attach user userg = UserGallery() userg.gallery = gal userg.mode = "W" userg.user = request.user userg.save() return redirect(gal.get_absolute_url()) else: return render_template("gallery/gallery/new.html", {"form": form}) else: form = GalleryForm() return render_template("gallery/gallery/new.html", {"form": form})
def new_gallery(request): ''' Creates a new gallery ''' if request.method == 'POST': form = GalleryForm(request.POST) if form.is_valid(): data = form.data # Creating the gallery gal = Gallery() gal.title = data['title'] gal.subtitle = data['subtitle'] gal.slug = slugify(data['title']) gal.pubdate = datetime.now() gal.save() # Attach user userg = UserGallery() userg.gallery = gal userg.mode = 'W' userg.user = request.user userg.save() return redirect(gal.get_absolute_url()) else: # TODO: add errors to the form and return it raise Http404 else: form = GalleryForm() return render_template('gallery/new_gallery.html', { 'form': form })
def set_avatar_from_file(self, avatar, filename='avatar.png'): """ Updates the avatar of this user from a file, creating a gallery on his account if needed and adding the avatar to the gallery. :param avatar: The avatar file (file-like object). :param filename: The file name, including the type extension. """ user_gallery = UserGallery.objects.filter(gallery__title=ZDS_APP['gallery']['avatars_gallery'], user=self.user)\ .first() if not user_gallery: gallery = Gallery() gallery.title = ZDS_APP['gallery']['avatars_gallery'] gallery.subtitle = '' gallery.slug = slugify(ZDS_APP['gallery']['avatars_gallery']) gallery.pubdate = datetime.now() gallery.save() user_gallery = UserGallery() user_gallery.gallery = gallery user_gallery.mode = GALLERY_WRITE user_gallery.user = self.user user_gallery.save() image = Image() image.title = _('Avatar') image.legend = _('Avatar importé') image.gallery = user_gallery.gallery image.physical = get_thumbnailer(avatar, relative_name=filename) image.pubdate = datetime.now() image.save() self.avatar_url = image.get_absolute_url()
def perform_create(self, title, user, subtitle=""): """Create gallery :param title: title :type title: str :param user: the user :type user: zds.member.models.User :param subtitle: subtitle :type subtitle: str :rtype: Gallery """ gallery = Gallery(title=title) gallery.subtitle = subtitle gallery.slug = slugify(title) gallery.pubdate = datetime.datetime.now() gallery.save() user_gallery = UserGallery(gallery=gallery, user=user, mode=GALLERY_WRITE) user_gallery.save() self.gallery = gallery self.users_and_permissions = {user.pk: {"read": True, "write": True}} return self.gallery
def form_valid(self, form): gallery = Gallery() gallery.title = form.cleaned_data['title'] gallery.subtitle = form.cleaned_data['subtitle'] gallery.slug = slugify(form.cleaned_data['title']) gallery.pubdate = datetime.now() gallery.save() # Attach user : userg = UserGallery() userg.gallery = gallery userg.mode = 'W' userg.user = self.request.user userg.save() return HttpResponseRedirect(gallery.get_absolute_url())