示例#1
0
def load_more_posts(request, page, query, search=False, hashtag=False):
	m = Microblog()
	page = int(page)
	listPost = None
	if search and query:
		if hashtag:
			query = "#%s" % (query)
		listPost = m.search_posts(query, page)
	else:
		if(not query):
			id = request.user.id
		else:
			id = int(query)
		listPost = m.get_posts(0, id, m.get_blog_user(request.user.id), page)

	return render_to_response('profile/post.html', {
			'request': request,
			'posts': listPost
			}, context_instance=RequestContext(request))
示例#2
0
def profile_posts(request, id, api=False, byid=False):
	m = Microblog()
	if(not id):
		if(not request.user.id):
			return HttpResponseRedirect("/auth/login")
		id = request.user.id
		user = request.user
	else:
		if byid:
			id = int(id)
			user = User.objects.get(pk=id)
		else:
			user = User.objects.get(username=id)
			id = user.id

	if request.method == 'POST':
		form = PostForm(request.POST)
		if form.is_valid():
			if request.META.get('HTTP_CONTEXT_URL'):
				try:
					geolocation = {
						'lat': float(request.META['HTTP_CONTEXT_GEO_LAT']),
						'lon': float(request.META['HTTP_CONTEXT_GEO_LON'])
					}
				except:
					geolocation = {
						'lat': 0.0,
						'lon': 0.0
					}
				try:
					url = request.META['HTTP_CONTEXT_URL']
					result = re.search('\/course\/(?P<slug>[a-zA-Z0-9-_]+)\/', url)
					course_slug = result.group('slug')
				except:
					course_slug = None
			else:
				try:
					geolocation = {
                    	'lat': float(request.POST['context_geo_lat']),
                    	'lon': float(request.POST['context_geo_lon'])
                	}
				except:
					geolocation = {
						'lat': 0.0,
						'lon': 0.0
					}

				course_slug = None

			extra = {
				'geolocation': geolocation,
				'course_slug': course_slug
			}
			m.insert_post(request.user.id, request.user.first_name, request.user.last_name, request.user.username, "https:" + gravatar_for_email(request.user.email), form.cleaned_data['postText'], extra)
			return HttpResponseRedirect("/user/posts")

	else:
		case = _getCase(request,id)

		blog_user = m.get_blog_user(request.user.id)
		if(blog_user and id in blog_user["following"]):
			following = "true"
		else:
			following = "false"

		followingCount = 0
		if(request.user.id != id):
			blog_user = m.get_blog_user(id)
			if(blog_user):
				followingCount = len(blog_user["following"])
		elif(blog_user):
			followingCount = len(blog_user["following"])

		listPost = m.get_posts(case, id, blog_user, 0)

		if not api:
			return render_to_response('profile/posts.html', {
				"id":id,
				"badges_count": get_db().get_collection('badge').find({"id_user": id}).count(),
				# "email":"@" + request.user.email.split("@")[0],
				'request': request,
				'form': PostForm(),
				'totalPost': m.count_posts(id),
				'posts': listPost,
				'case': case,
				"user_view_profile": user,
				"following": following,
				"followingCount": followingCount,
				"followerCount": m.get_num_followers(id)
				}, context_instance=RequestContext(request))
		else:
			response = {
				"id": id,
				"user": '******' % (user.username),
				"following": following,
				"followingCount": followingCount,
				"followerCount": m.get_num_followers(id),
				"posts": listPost
			}
			return HttpResponse(json_util.dumps(response), mimetype='application/json')