def create_post(request): if request.POST: # get POST data body = request.POST['body'] author = request.user apartment_id = author.tenant.apartment.id # create new Post post = Post(body=body, author=author, apartment_id=apartment_id) post.save() return HttpResponseRedirect('/stream/') else: return HttpResponse('<p>here</p>')
def delete_post(request, id): post = Post.objects.get(id=id) Post.delete(post) return HttpResponseRedirect('/stream/')
def post_save(request): """ This function post_save will save user's posts in db. @version 0.6 """ result = {} news = request.POST['post'] news = news.strip() try: img_s = request.POST['img'] img = img_s.replace('/s/', '/o/') img_m = img_s.replace('/s/', '/m/') print 'img is:', img except: img = img_s = img_m = '' try: url = request.POST['url'] except: url = '' if len(news) > 600: result = { "status": False, "msg": "At most enter 600 characters." } return JsonResponse(result) else: news = re.sub('<', '<', news) # for tag <a>|</a> news = re.sub('>', '>', news) for m in re.finditer('https?://\S+|www\.\S+', news): news = re.sub(m.group(0), (r'<a href="%s">%s</a>') % (m.group(0), m.group(0)), news, count=1) print 'news after replace: ', news UserObj = User.objects.get(username = request.user) BaseObj = Base.objects.get(user=request.user) PostInsert = Post(post_content=news, img=img, img_s=img_s, img_m=img_m, url=url, comment_count=0, user_id=UserObj) PostInsert.save(force_insert=True) # _put_wall_post(request, news) post_id = PostInsert.id print "Insert Success!" PostObj = Post.objects.get(id=post_id) print(str(PostObj.post_date)) result = { "status": True, "id": PostObj.id, "content": PostObj.post_content, "date": str(PostObj.post_date), "humandate": humantime(PostObj.post_date), "img_s": str(PostObj.img_s), "img_m": str(PostObj.img_m), "url": PostObj.url, "name":_show_name(request), "imagephoto": settings.MEDIA_URL + str(BaseObj.imagesmall), } return JsonResponse(result)