def testCredits(self): jane = Credit.objects.create(first_names='Jane', last_name='Bloggs') joe = Credit.objects.create(first_names='Joe', last_name='Smith') lisa = Credit.objects.create(first_names='Lisa', last_name='Doe') tom = Credit.objects.create(first_names='Tom', last_name='Thumb') p = BasicPost.objects.filter(pk=3).select_subclasses()[0] o = OrderedCredit(content_object=p, credit=jane, position=3) o.save() o = OrderedCredit(content_object=p, credit=joe, position=2) o.save() o = OrderedCredit(content_object=p, credit=lisa, position=5) o.save() o = OrderedCredit(content_object=p, credit=tom, position=1) o.save() authors = BasicPost.objects.select_subclasses().filter( pk=3)[0].get_authors() self.assertEquals(authors, "Tom Thumb, Joe Smith, Jane Bloggs and Lisa Doe") p.authors.all().delete() o = OrderedCredit(content_object=p, credit=lisa, position=0) o.save() authors = BasicPost.objects.select_subclasses().filter( pk=3)[0].get_authors() self.assertEquals(authors, "Lisa Doe") p.authors.all().delete() o = OrderedCredit(content_object=p, credit=joe, position=1) o.save() o = OrderedCredit(content_object=p, credit=jane, position=0) o.save() authors = BasicPost.objects.select_subclasses().filter( pk=3)[0].get_authors() self.assertEquals(authors, "Jane Bloggs and Joe Smith") p.authors.all().delete() wikipedia = Credit.objects.create( is_person=False, first_names="First names should be ignored", last_name="Wikipedia") o = OrderedCredit(content_object=p, credit=jane, position=0) o.save() o = OrderedCredit(content_object=p, credit=wikipedia, position=1) o.save() authors = BasicPost.objects.select_subclasses().filter( pk=3)[0].get_authors() self.assertEquals(authors, "Jane Bloggs and Wikipedia")
def testCredits(self): jane = Credit.objects.create(first_names='Jane', last_name='Bloggs') joe = Credit.objects.create(first_names='Joe', last_name='Smith') lisa = Credit.objects.create(first_names='Lisa', last_name='Doe') tom = Credit.objects.create(first_names='Tom', last_name='Thumb') p = BasicPost.objects.filter(pk=3).select_subclasses()[0] o = OrderedCredit(content_object=p, credit=jane, position=3) o.save() o = OrderedCredit(content_object=p, credit=joe, position=2) o.save() o = OrderedCredit(content_object=p, credit=lisa, position=5) o.save() o = OrderedCredit(content_object=p, credit=tom, position=1) o.save() authors = BasicPost.objects.select_subclasses().filter(pk=3)[0].get_authors() self.assertEquals(authors, "Tom Thumb, Joe Smith, Jane Bloggs and Lisa Doe") p.authors.all().delete() o = OrderedCredit(content_object=p, credit=lisa, position=0) o.save() authors = BasicPost.objects.select_subclasses().filter(pk=3)[0].get_authors() self.assertEquals(authors, "Lisa Doe") p.authors.all().delete() o = OrderedCredit(content_object=p, credit=joe, position=1) o.save() o = OrderedCredit(content_object=p, credit=jane, position=0) o.save() authors = BasicPost.objects.select_subclasses().filter(pk=3)[0].get_authors() self.assertEquals(authors, "Jane Bloggs and Joe Smith") p.authors.all().delete() wikipedia = Credit.objects.create(is_person=False, first_names="First names should be ignored", last_name="Wikipedia") o = OrderedCredit(content_object=p, credit=jane, position=0) o.save() o = OrderedCredit(content_object=p, credit=wikipedia, position=1) o.save() authors = BasicPost.objects.select_subclasses().filter(pk=3)[0].get_authors() self.assertEquals(authors, "Jane Bloggs and Wikipedia")
def submit_article(request): ''' View for Article Submission ''' ImageFormset = formset_factory(ImageForm, extra=settings.MAX_NUM_IMAGES) if request.method == 'POST': form = ArticleSubmissionForm(request.POST, request.FILES) image_forms = ImageFormset(request.POST, request.FILES) if form.is_valid() and image_forms.is_valid(): title = request.POST['title'] subtitle = request.POST['subtitle'] body = request.POST['body'] editor = request.POST['editor'] authors = request.POST.get('authors', []) tags = request.POST.get('tags', []) files = request.FILES post_body = EnhancedText( body, editor) #Combines body and editor field to for creating post post = None if len(files) == 0: #Will save post as basic post post = BasicPost(title=title, slug=slugify(title), subtitle=subtitle, body=post_body) post.slug = post._get_unique_slug() post.save() elif len(files) == 1: #Will save post as post with simple image image = None image = files.itervalues().next() post = PostWithSimpleImage(title=title, slug=slugify(title), subtitle=subtitle, body=post_body, image=image) post.save() else: #Will save post as post with slideshow gallery = Gallery(title=title) gallery.save() path = os.path.join(settings.MEDIA_ROOT, 'uploads') path = os.path.join(path, 'images') for index, image in enumerate(files): filename_unique = False filename = os.path.join(path, files[image].name) counter = 1 while not filename_unique: if os.path.exists(filename): filename_split = filename.split('.') filename = filename_split[0] extension = filename_split[1] filename = filename + unicode( counter) + '.' + extension else: filename_unique = True image_file = open(filename, 'wb+') for chunk in files[image].chunks(): image_file.write(chunk) image_file.close() filename_split = filename.split(os.sep) base_filename = filename_split[-1] filename = os.path.join('uploads', 'images') filename = os.path.join(filename, base_filename) image = Image(title=title, slug=slugify(title), file=filename) image.slug = image._get_unique_slug() image.save() ordered_image = OrderedImage(gallery=gallery, image=image, position=index) ordered_image.save() post = PostWithSlideshow(title=title, slug=slugify(title), subtitle=subtitle, body=post_body, gallery=gallery) post.save() if post: # Saves the authors and tags of the post for index, author in enumerate(authors): credit = OrderedCredit( credit=Credit.objects.get(id=author), content_object=post, position=index) credit.save() for index, tag in enumerate(tags): tag = TaggedItem(tag=Tag.objects.get(id=tag), object=post) tag.save() article = SubmittedArticle(submitted_by=request.user, object=post) article.save() return HttpResponseRedirect(reverse('submit_article_success')) else: form = ArticleSubmissionForm() image_forms = ImageFormset() return render_to_response('submit_article/add.html', { 'form': form, 'image_forms': image_forms, 'max_forms': settings.MAX_NUM_IMAGES }, context_instance=RequestContext(request))
def submit_article(request): ''' View for Article Submission ''' ImageFormset = formset_factory(ImageForm, extra=settings.MAX_NUM_IMAGES) if request.method == 'POST': form = ArticleSubmissionForm(request.POST, request.FILES) image_forms = ImageFormset(request.POST, request.FILES) if form.is_valid() and image_forms.is_valid(): title = request.POST['title'] subtitle = request.POST['subtitle'] body = request.POST['body'] editor = request.POST['editor'] authors = request.POST.get('authors', []) tags = request.POST.get('tags', []) files = request.FILES post_body = EnhancedText(body, editor) #Combines body and editor field to for creating post post = None if len(files) == 0: #Will save post as basic post post = BasicPost(title=title, slug=slugify(title), subtitle=subtitle, body=post_body) post.slug = post._get_unique_slug() post.save() elif len(files) == 1: #Will save post as post with simple image image = None image = files.itervalues().next() post = PostWithSimpleImage(title=title, slug=slugify(title), subtitle=subtitle, body=post_body, image=image) post.save() else: #Will save post as post with slideshow gallery = Gallery(title=title) gallery.save() path = os.path.join(settings.MEDIA_ROOT, 'uploads') path = os.path.join(path, 'images') for index, image in enumerate(files): filename_unique = False filename = os.path.join(path, files[image].name) counter = 1 while not filename_unique: if os.path.exists(filename): filename_split = filename.split('.') filename = filename_split[0] extension = filename_split[1] filename = filename + unicode(counter) + '.' + extension else: filename_unique = True image_file = open(filename, 'wb+') for chunk in files[image].chunks(): image_file.write(chunk) image_file.close() filename_split = filename.split(os.sep) base_filename = filename_split[-1] filename = os.path.join('uploads', 'images') filename = os.path.join(filename, base_filename) image = Image(title=title, slug=slugify(title), file=filename) image.slug = image._get_unique_slug() image.save() ordered_image = OrderedImage(gallery=gallery, image=image, position=index) ordered_image.save() post = PostWithSlideshow(title=title, slug=slugify(title), subtitle=subtitle, body=post_body, gallery=gallery) post.save() if post: # Saves the authors and tags of the post for index, author in enumerate(authors): credit = OrderedCredit(credit=Credit.objects.get(id=author), content_object=post, position=index) credit.save() for index, tag in enumerate(tags): tag = TaggedItem(tag=Tag.objects.get(id=tag), object=post) tag.save() article = SubmittedArticle(submitted_by=request.user, object=post) article.save() return HttpResponseRedirect(reverse('submit_article_success')) else: form = ArticleSubmissionForm() image_forms = ImageFormset() return render_to_response('submit_article/add.html', {'form': form, 'image_forms': image_forms, 'max_forms': settings.MAX_NUM_IMAGES}, context_instance=RequestContext(request))