def newentry(request): throw_404_unless_allowed(request.user) if request.method == 'POST': form = blog_form(request.POST) if form.is_valid(): Entry = form.save(commit=False) Entry.publisher = request.user Entry.save() return HttpResponseRedirect('/blogs/admin') else: form = blog_form() return render_to_response("blog/new_blog.html", {"form": form}, context_instance=RequestContext(request))
def from_feed(source): """ Imports blog entries from an external site via RSS. """ status = 'attempting to import from feed %s<br>' % source.feed_url d = feedparser.parse(source.feed_url) # Match to blog try: blog = Blog.objects.get(title=source.title) except: blog = create_blog(source.title) for item in d.entries: dupe = False # Set our dupe flag for the following loop try: existing = Entry.objects.get(title=item.title) dupe = True except: if dupe==False: cleanbody = strip_tags(item.description).replace('\n', '\n\n') try: i = Entry( title = item.title, slug = slugify(item.title), blog = blog, summary = cleanbody[:200].rsplit(' ', 1)[0]+'...', body = item.description, published = True ) i.save() except: pass if item.has_key('media_content'): for m in item.media_content: url = m['url'] try: caption = item.media_description except: caption = ''; filename = url.rsplit('/', 1)[1] copy_file(url, filename, blog.slug) img = Image(image="img/blogs/"+ blog.slug + '/' + filename, caption= caption, entry=i) img.save() status += "done"
def create_entry(request): if request.method == 'GET': return render(request, 'blogs/create_entry.html') elif request.method == 'POST': if request.POST['title'] == '' or request.POST['contents'] == '': messages.error(request, u'Field(s) empty error') return HttpResponseRedirect(reverse('blogs:create_entry')) entry = Entry() entry.user = request.user entry.title = request.POST['title'] entry.contents = request.POST['contents'] entry.pub_date = timezone.now() entry.save() if request.POST['tags'] != '': tags = request.POST['tags'].split() for tag in tags: entry.tags.add(Tag.objects.get_or_create(name=tag)[0]) entry.save() return HttpResponseRedirect(reverse('blogs:index'))
def do_post_entry(request, **blog_pk): # TODO: blog_pkがログインユーザーのものでない場合、トップ画面にリダイレクトする if request.method == "GET": target_blog = Blog.objects.filter(pk=blog_pk["blog_pk"]) if len(target_blog) > 0 and target_blog[0].user.username == request.session["user_id"]: entry_post_form = EntryPostForm(initial={"target_blog_pk": blog_pk["blog_pk"]}) return render(request, "blog_entry.html", {"entry_post_form": entry_post_form}) else: return redirect("ref_blog_top") elif request.method == "POST": entry_post_form = EntryPostForm(request.POST) if entry_post_form.is_valid(): _subject = entry_post_form.cleaned_data["subject"] _body = entry_post_form.cleaned_data["body"] _blog_pk = entry_post_form.cleaned_data["target_blog_pk"] _target_blog = Blog.objects.filter(pk=_blog_pk) new_entry = Entry() new_entry.subject = _subject new_entry.body = _body new_entry.blog = _target_blog[0] new_entry.save() return redirect("ref_blog_top")
def setUp(self): entries = [ Entry() ] * 4 # create four intance of Enty and stores them in a list. self.url = self.client.get('/lastest_entries/', {'entries': entries})