Esempio n. 1
0
def create(request):
	if request.user.is_authenticated() is False:
		args = {}
		args['entry_list'] = Entry.objects.all()
		args['error'] = 'You must be signed in before creating a new post.'	
		return render_to_response('coltrane/entry_index.html', args)
	if request.POST:
		#post_values = request.POST.copy()
		#post_values['author'] = 4
		post_values = request.POST.copy()
		form = EntryForm(request.POST, request.FILES)
		if form.is_valid():
			#form.save()
			obj = form.save(commit=False)
			obj.author = request.user
			#get entry and category id:
			e1 = obj.id
			c1 = post_values['categories']
			c2 = Category.objects.get(id=c1)
			#save entry to category	
			obj.save()	
			#add the hashtags from the post into the database:
			getHashtags = obj.body + " " + obj.title
			s = {tag.strip("#") for tag in getHashtags.split() if tag.startswith("#")}
			for item in s:
				h = Hashtag.objects.filter(title=item)
				if not h:
					item = item.lower()
					newhash = Hashtag(title=item, num_used=1)
					newhash.save()
					obj.tags.add(newhash)
					#obj.save()
				else:
					#oldh = Hashtag.objects.get(title=item)	
					for item2 in h:
						item2.num_used += 1
						item2.save()
						obj.tags.add(item2)			
					#obj.save()	
			#this code increments each time a specific user creates a post
			inc_post = request.user.id
			count_post = User_Profile.objects.get(uid_id=inc_post)
			count_post.num_posts += 1
			count_post.save()
			
			obj.categories.add(c1)
			return HttpResponseRedirect('/weblog')
	else:
		form = EntryForm()
		form.author = request.user
	args = {}
	args.update(csrf(request))
	args['form'] = form 
	args['username'] = request.user.username
	args['uid'] = request.user.id
	return render_to_response('coltrane/newpost.html', args)
Esempio n. 2
0
def create_url(request):
	if request.user.is_authenticated() is False:
		args = {}
		args['entry_list'] = Entry.objects.all()
		args['error'] = 'You must be signed in before creating a new post.'
		return render_to_response('coltrane/entry_index.html', args)
	if request.POST:
		post_values = request.POST.copy()
		form = EntryFormURL(request.POST, request.FILES)
		if form.is_valid():
			obj = form.save(commit=False)
			obj.author = request.user		
			e1 = obj.id
			c1 = post_values['categories']
			c2 = Category.objects.get(id=c1)					
			obj.save()					
			getHashtags = obj.body + " " + obj.title
			s = {tag.strip("#") for tag in getHashtags.split() if tag.startswith("#")}
			for item in s:
				h = Hashtag.objects.filter(title=item)
				if not h:
					item = item.lower()
					newhash = Hashtag(title=item, num_used=1)
					newhash.save()
					obj.tags.add(newhash)
				else:
					for item2 in h:
						item2.num_used += 1
						item2.save()
						obj.tags.add(item2)
			inc_post = request.user.id
			count_post = User_Profile.objects.get(uid_id=inc_post)
			count_post.num_posts += 1
			count_post.save()	
			obj.categories.add(c1)
			return HttpResponseRedirect('/weblog')
	else:
		form = EntryFormURL()
		form.author = request.user
	args = {}
	args.update(csrf(request))
	args['form'] = form 
	args['username'] = request.user.username
	args['uid'] = request.user.id
	return render_to_response('coltrane/newpostURL.html', args)