def modify_important(request): ''' 修改精选 ''' qb = QuestionBase() img_alt = request.REQUEST.get('imgAlt', '') sort_num = request.REQUEST.get('sort', 0) question_id = request.REQUEST.get('questionId') question = qb.get_question_by_id(question_id) img = request.FILES.get('importantImg') iq = qb.get_important_question_by_question_id(question_id) img_name = iq.img # 如果有上传图片 if img: flag, img_name = qiniu_client.upload_img(img, img_type='important') img_name = '%s/%s' % (settings.IMG0_DOMAIN, img_name) title = request.REQUEST.get('title') summary = request.REQUEST.get('summary') author = request.REQUEST.get('author') author_id = None # 作者 if author: author = UserBase().get_user_by_nick(author) if author: author_id = author.id code, msg = qb.set_important(question, request.user, title, summary, author_id, img_name, img_alt, sort_num) url = '/admin/question/important_question#modify/%s' % iq.question.id return HttpResponseRedirect(url)
def add_important(request): ''' 添加精选 ''' img = request.FILES.get('importantImg') flag, img_name = qiniu_client.upload_img(img, img_type='important') qb = QuestionBase() question_id = request.REQUEST.get('questionId') question = qb.get_question_by_id(question_id) img_alt = request.REQUEST.get('imgAlt', '') sort_num = request.REQUEST.get('sort', 0) title = request.REQUEST.get('title') summary = request.REQUEST.get('summary') author = request.REQUEST.get('author') author_id = None # 作者 if author: author = UserBase().get_user_by_nick(author) if author: author_id = author.id code, msg = qb.set_important(question, request.user, title, summary, author_id, '%s/%s' % (settings.IMG0_DOMAIN, img_name), img_alt, sort_num) url = '' if code == 0: iq = qb.get_important_question_by_question_id(question_id) url = '/admin/question/important_question#modify/%s' % iq.question.id else: url = request.META['HTTP_REFERER'] return HttpResponseRedirect(url)
def get_important_question_by_question_id(request): question_id = request.REQUEST.get('question_id') data = '' important_question = QuestionBase().get_important_question_by_question_id(question_id) if important_question: author = None if important_question.author_user_id: author = UserBase().get_user_by_id(important_question.author_user_id) data = { 'question_id': important_question.question.id, 'user_id': important_question.question.user.id, 'user_nick': important_question.question.user.nick, 'title': important_question.title, 'summary': important_question.summary, 'author': author.nick if author else '', 'img': important_question.img, 'img_alt': important_question.img_alt, 'sort_num': important_question.sort_num, 'create_time': str(important_question.question.create_time), 'set_important_time': str(important_question.create_time) } return HttpResponse(json.dumps(data), mimetype='application/json')
def create_invite(self, from_user_id, to_user_id, question_id): try: from www.question.interface import QuestionBase ub = UserBase() try: from_user = ub.get_user_by_id(from_user_id) to_user = ub.get_user_by_id(to_user_id) question = QuestionBase().get_question_by_id(question_id) assert from_user and to_user and question except: transaction.rollback(using=DEFAULT_DB) return 99800, dict_err.get(99800) if from_user_id == to_user_id: transaction.rollback(using=DEFAULT_DB) return 40100, dict_err.get(40100) # 同一个问题最多邀请6个人 if InviteAnswerIndex.objects.filter(from_user_id=from_user_id, question_id=question_id).count() >= 6: transaction.rollback(using=DEFAULT_DB) return 40101, dict_err.get(40101) # 重复邀请给出提示 if InviteAnswerIndex.objects.filter(from_user_id=from_user_id, to_user_id=to_user_id, question_id=question_id): transaction.rollback(using=DEFAULT_DB) return 40102, dict_err.get(40102) try: ia = InviteAnswer.objects.create(from_user_ids=json.dumps([from_user_id, ]), to_user_id=to_user_id, question_id=question_id) need_update_unread_count = True except: ia = InviteAnswer.objects.get(to_user_id=to_user_id, question_id=question_id) from_user_ids = json.loads(ia.from_user_ids) if from_user_id not in from_user_ids: from_user_ids.append(from_user_id) ia.from_user_ids = json.dumps(from_user_ids) ia.save() need_update_unread_count = True if ia.is_read else False # 建立索引 InviteAnswerIndex.objects.create(from_user_id=from_user_id, to_user_id=to_user_id, question_id=question_id) # 更新未读消息,新邀请或者邀请已读才更新未读数 if need_update_unread_count: UnreadCountBase().update_unread_count(to_user_id, code='invite_answer') # 发送提醒邮件 context = dict(user=from_user, question=question) async_send_email(to_user.email, u'%s 在智选邀请你回答问题' % (from_user.nick, ), utils.render_email_template('email/invite.html', context), 'html') transaction.commit(using=DEFAULT_DB) return 0, dict_err.get(0) except Exception, e: debug.get_debug_detail(e) transaction.rollback(using=DEFAULT_DB) return 99900, dict_err.get(99900)
def format_user_received_invites(self, user_received_invites): from www.question.interface import QuestionBase ub = UserBase() for uri in user_received_invites: uri.question = QuestionBase().get_question_summary_by_id(uri.question_id) uri.from_users = [ub.get_user_by_id(user_id) for user_id in json.loads(uri.from_user_ids)] uri.from_users.reverse() return user_received_invites
def search(request): ''' 分页查询提问,可以根据标题过滤 ''' title = request.REQUEST.get('title') page_index = int(request.REQUEST.get('page_index', 1)) order = request.REQUEST.get('order', 'create_time') if title: questions = QuestionBase().get_question_by_title(title) else: questions = QuestionBase().get_all_questions_by_order_type(order) page_objs = page.Cpt(questions, count=10, page=page_index).info data = [] num = 10 * (page_index - 1) + 0 for question in page_objs[0]: num += 1 data.append({ 'num': num, 'question_id': question.id, 'user_id': question.user.id, 'user_nick': question.user.nick, 'title': question.title, 'description': question.get_summary(), 'view_count': question.views_count, 'answer_count': question.answer_count, 'is_important': question.is_important, 'is_hide_user': question.is_hide_user, 'state': question.state, 'created_time': str(question.create_time) }) return HttpResponse(json.dumps({ 'data': data, 'page_count': page_objs[4], 'total_count': page_objs[5] }), mimetype='application/json')
def statistics_questions(request): ''' 提问统计 ''' start_date = request.REQUEST.get('start_date') end_date = request.REQUEST.get('end_date') data = common_statistics( start_date, end_date, QuestionBase().get_questions_by_range_date(start_date + ' 00:00:00', end_date + ' 23:59:59')) return HttpResponse(json.dumps(data), mimetype='application/json')
def get_question_by_id(request): ''' 根据提问id查询提问信息 ''' question_id = request.REQUEST.get('question_id') data = '' question = QuestionBase().get_question_by_id(question_id) if question: data = { 'question_id': question.id, 'question_title': question.title, 'question_desc': question.content } return HttpResponse(json.dumps(data), mimetype='application/json')
def user_questions(request, user_id, template_name='account/user_questions.html'): ''' 提问 - 个人主页 ''' from www.question.interface import QuestionBase user_key_words = request.REQUEST.get('user_key_words', '').strip() user = user_id # 装饰器转换了对象 qb = QuestionBase() if not user_key_words: questions = qb.get_questions_by_user_id(user.id) else: questions = qb.search_user_questions(user.id, user_key_words) # 分页 page_num = int(request.REQUEST.get('page', 1)) page_objs = page.Cpt(questions, count=10, page=page_num).info questions = page_objs[0] page_params = (page_objs[1], page_objs[4]) questions = qb.format_quesitons(questions) return render_to_response(template_name, locals(), context_instance=RequestContext(request))
def get_hotest_response(self, to_user, from_user): items = '' for question in QuestionBase().get_all_important_question()[:4]: items += (self.get_base_news_item_response() % dict(title=question.iq_title.replace('%', '%%'), des='', picurl=question.img, hrefurl='%s%s' % (settings.MAIN_DOMAIN, question.get_url()))) base_xml = self.get_base_news_response(items) return base_xml % dict(to_user=from_user, from_user=to_user, timestamp=int(time.time()), articles_count=4)
def get_response(self, xml): from pyquery import PyQuery as pq xml = self.format_input_xml(xml) jq = pq(xml) to_user = jq('tousername')[0].text from_user = jq('fromusername')[0].text events = jq('event') app_key = self.get_app_key_by_app_type(to_user) logging.error(u'收到一个来自app:%s 的请求' % app_key) # click事件 if events: event = events[0].text.lower() if event in ('subscribe', ): return self.get_subscribe_event_response(to_user, from_user) elif event in ('click'): event_key = jq('eventkey')[0].text.lower() if event_key == 'hotest': return self.get_hotest_response(to_user, from_user) # 语音识别上传 recognitions = jq('recognition') if recognitions: recognition = recognitions[0].text.lower() logging.error(u'收到用户发送的语音数据,内容如下:%s' % recognition) if u'干货' in recognition or u'精选' in recognition or u'来一发' in recognition or u'资讯' in recognition: questions = QuestionBase().get_all_important_question() question = questions[random.randint(0, len(questions) - 1)] items = self.get_base_news_item_response() % dict( title=question.iq_title.replace('%', '%%'), des='', picurl=question.img, hrefurl='%s%s' % (settings.MAIN_DOMAIN, question.get_url())) return self.get_base_news_response(items) % dict( to_user=from_user, from_user=to_user, timestamp=int(time.time()), articles_count=1) # 文字识别 msg_types = jq('msgtype') if msg_types: msg_type = msg_types[0].text if msg_type == 'text': content = jq('content')[0].text.strip() logging.error(u'收到用户发送的文本数据,内容如下:%s' % content)
def baidu_map(request, template_name="other/baidu_map.html"): ''' @note: 网站地图,给百度准备 ''' from www.question.interface import QuestionBase from www.stock.interface import StockFeedBase now = datetime.datetime.now().date() questions = QuestionBase().get_questions_by_range_date( now, now + datetime.timedelta(days=1)) stock_feeds = StockFeedBase().get_all_stock_feeds()[:10] return render_to_response(template_name, locals(), context_instance=RequestContext(request))
def format_feeds_by_id(self, feed_ids): from www.question.interface import QuestionBase, AnswerBase from www.stock.interface import StockFeedBase feeds = [] for feed_id in feed_ids: feed = self.get_feed_by_id(feed_id) dict_feed = dict(feed_id=feed.id, create_time=feed.create_time.strftime('%Y-%m-%d %H:%M:%S'), feed_type=feed.feed_type) if feed.source == 0: # 用户产生的动态 user = UserBase().get_user_by_id(feed.user_id) obj_info = dict(user_id=feed.user_id, user_avatar=user.get_avatar_65(), user_nick=user.nick) if feed.feed_type in (1,): obj_info.update(QuestionBase().get_question_summary_by_id(feed.obj_id)) elif feed.feed_type in (2, 3): obj_info.update(AnswerBase().get_answer_summary_by_id(feed.obj_id)) if feed.source == 1: # 股票产生的动态 obj_info = StockFeedBase().get_stock_feed_summary_by_id(feed.obj_id) dict_feed.update(obj_info) feeds.append(dict_feed) return feeds
def update_user_count(): from www.account.models import UserCount, User from www.question.interface import QuestionBase, AnswerBase, LikeBase from www.timeline.interface import UserFollowBase for user in User.objects.all(): user_id = user.id user_question_count = QuestionBase().get_user_question_count(user_id) user_answer_count = AnswerBase().get_user_sended_answers_count(user_id) user_liked_count = LikeBase().get_user_liked_count(user_id) following_count = UserFollowBase().get_following_count(user_id) follower_count = UserFollowBase().get_follower_count(user_id) if user_question_count or user_answer_count or user_liked_count or following_count or follower_count: user_count, created = UserCount.objects.get_or_create( user_id=user_id) user_count.user_question_count = user_question_count user_count.user_answer_count = user_answer_count user_count.user_liked_count = user_liked_count user_count.following_count = following_count user_count.follower_count = follower_count user_count.save() print 'ok'
def get_important_question_by_title(request): title = request.POST.get('title') page_index = int(request.POST.get('page_index', 1)) important_questions = QuestionBase().get_important_question_by_title(title) page_objs = page.Cpt(important_questions, count=10, page=page_index).info data = [] num = 10 * (page_index - 1) + 0 for important_question in page_objs[0]: num += 1 author = None if important_question.author_user_id: author = UserBase().get_user_by_id(important_question.author_user_id) data.append({ 'num': num, 'question_id': important_question.question.id, 'user_id': important_question.question.user.id, 'user_nick': important_question.question.user.nick, 'title': important_question.title, 'summary': important_question.summary, 'author': author.nick if author else '', 'img': important_question.img, 'img_alt': important_question.img_alt, 'sort_num': important_question.sort_num, 'create_time': str(important_question.question.create_time), 'set_important_time': str(important_question.create_time) }) return HttpResponse( json.dumps({'data': data, 'page_count': page_objs[4], 'total_count': page_objs[5]}), mimetype='application/json' )
def async_add_question_view_count(question_id): ''' @note: 异步更新浏览次数 ''' from www.question.interface import QuestionBase QuestionBase().add_question_view_count(question_id)
def cancel_important(request): question_id = request.REQUEST.get('question_id') qb = QuestionBase() question = qb.get_question_by_id(question_id) return qb.cancel_important(question, request.user)