def post_handler(request,**kwargs): context = {} if request.method == 'GET': if 'post_id' in kwargs: instance = Post.objects(post_id=kwargs['post_id']) form = PostCreationForm(instance=instance) else: form = PostCreationForm() context['form'] = form return render_to_response('web/addpost.html', RequestContext(request,context)) elif request.method == 'POST': if 'post_id' in kwargs: instance = Post.objects(post_id=kwargs['post_id']) form = PostCreationForm(request.POST,instance=instance) else: print request body = request.POST['body'] user = kwargs['user'] if "file" in request.FILES.items(): print "Got File!!!" post = add_post(user,file,body) else: post = add_post(user,body) if post: url = '/user/'+user+'/dashboard/' return redirect(url) else: context['form'] = PostCreationForm() return render_to_response('web/addpost.html', RequestContext(request,context)) return render_to_response('web/addpost.html', RequestContext(request,context))
def _insert(item, p_date): keys = ('title', 'author', 'content_url', 'digest', 'cover', 'source_url') sub_data = utils.sub_dict(item, keys) post = Post(**sub_data) p_date = datetime.fromtimestamp(p_date) post['p_date'] = p_date logger.info('save %s' % post.title) try: post.save() except Exception as e: logger.error('fail to save %s' % post.to_json(), exc_info=True)
def add_comment(post_id,body,user): try: post = Post.objects(post_id=post_id).first() print body comment = Comment(body=body,user=User.objects(username=user).first()) print comment.body post.comment.append(comment) post.save() return post except Exception,error: print "function error %s"%error return None
def save(self, commit=True): post = Post() post.user = self.cleaned_data['user'] post.body = self.cleaned_data['body'] fs.save(self.cleaned_data['file'].name, self.cleaned_data['file']) post.file = self.cleaned_data['file'] post.save()
def add_post(user,body,file=None): try: post = Post() post.body = body post.user = User.objects(username=user).first() print file if file: post.file = file fs.save(post.file,file.read()) post.save() return post except Exception,error: print error return None
def dashboard(request,**kwargs): context = {} if request.method == 'GET': user = kwargs['user'] posts = Post.objects().order_by(user) context['posts'] = posts context['username'] = user context['form'] = CommentForm() return render_to_response('web/dashboard.html', RequestContext(request,context)) elif request.method == 'POST': body = request.POST['body'] user = kwargs['user'] post_id = request.POST['comment'] print post_id comments = add_comment(post_id,body,user) context['form'] = CommentForm() if comments: url = '/user/'+user+'/dashboard/' return redirect(url) else: return render_to_response('web/dashboard.html', RequestContext(request,context))
import time from crawler import WeiXinCrawler from document import Post if __name__ == '__main__': for post in Post.objects(reward_num=0): WeiXinCrawler.update_post(post) time.sleep(5)