def add_comments(request): data = {'status': 0, 'info': 'error'} try: # create new comment. authkey = request.REQUEST['creator_authkey'] auth = Auth.objects.get(key=authkey) comment_creator = json.loads(auth.data) # this return "an array" with the properties in an User object. creator_ = User.objects.get(id=comment_creator['id']) comment_creat_time = request.REQUEST['create_time_'] comment_content = request.REQUEST['content_'] # problem is here............................................. comment_ = Comment(creator=creator_, create_time=comment_creat_time, content=comment_content) comment_.save() # add the comment to the cooresponding spot spot_id = request.REQUEST['id'] spot_ = Spot.objects.get(id=spot_id) spot_.comments.add(comment_) # comment_ must be saved first, as add() only connects them without any copy. spot_.save() data['comment'] = comment_.toJsonFormat() # to show the comment_ info in the new comment. #data['user'] = creator_.toJsonFormat() data['spot'] = spot_.toJsonFormat() data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def user(request): ''' 获取个人信息,必须登陆后才行 参数: 返回值: 没有任何异常,则 {'status': 1, 'info': 'ok', 'data': 用户的信息} 否则 {‘status': 0, 'info': 'error'} ''' data = {'status': 0, 'info': 'error'} try: authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) data['data'] = json.loads(auth.data) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def discover_topic(request): data = {'status': 0, 'info': 'error'} try: # get the current user from database authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads(auth.data) # this return "an array" with the properties in an User object. user_ = User.objects.get(id=cur_user['id']) # set iTopic list and iTopic count iTopic_list_ = user_.focus.all() iTopic_count_ = user_.focus.count() data['iTopic_count'] = iTopic_count_ data['iTopic_list'] = [] for it in iTopic_list_: data['iTopic_list'].append(it.toJsonFormat()) # set rTopic list and rTopic count system_user = User.objects.get(username='******') rTopic_list_ = Topic.objects.filter(creator=system_user) rTopic_count_ = Topic.objects.filter(creator=system_user).count() data['rTopic_count'] = rTopic_count_ data['rTopic_list'] = [] for rt in rTopic_list_: data['rTopic_list'].append(rt.toJsonFormat()) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def home_receiveComment(request): data = {'status': 0, 'info': 'error'} try: # get current user authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads( auth.data ) # this return "an array" with the properties in an User object. user_ = User.objects.get(id=cur_user['id']) data['cur_user'] = user_.toJsonFormat() # the comment need to corespond to a spot, so we need to find out the comment and spot together own_spots = Spot.objects.filter(creator=user_) data['spots_count'] = own_spots.count() data['cor_spots'] = [] for cs in own_spots: data['cor_spots'].append( cs.toJsonFormat(show_comment=True, start=0, end=10)) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def createTopic(request): data = {'status': 0, 'info': 'error'} try: authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads( auth.data) # this is the json format User, not a class type User user_ = User.objects.get(id=cur_user['id']) create_time = request.REQUEST['create_time'] title = request.REQUEST['title'] begin_time = request.REQUEST['startTime'] end_time = request.REQUEST['endTime'] category = request.REQUEST['category'] new_topic = Topic(creator=user_, create_time=create_time, category=category, title=title, begin_time=begin_time, end_time=end_time) new_topic.save() data['topicID'] = new_topic.id data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def home_receiveComment(request): data = {'status': 0, 'info': 'error'} try: # get current user authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads(auth.data) # this return "an array" with the properties in an User object. user_ = User.objects.get(id=cur_user['id']) data['cur_user'] = user_.toJsonFormat() # the comment need to corespond to a spot, so we need to find out the comment and spot together own_spots = Spot.objects.filter(creator=user_) data['spots_count'] = own_spots.count() data['cor_spots'] = [] for cs in own_spots: data['cor_spots'].append(cs.toJsonFormat(show_comment=True, start=0, end=10)) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def home_ownComment(request): data = {'status': 0, 'info': 'error'} try: # get current user authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads(auth.data) # this return "an array" with the properties in an User object. user_ = User.objects.get(id=cur_user['id']) # the comment need to corespond to a spot, so we need to find out the comment and spot together comment_list = Comment.objects.filter(creator=user_) comment_count = Comment.objects.filter(creator=user_).count() data['comment_count'] = comment_count # as the comment_list and cor_spots in the correct coresponding order, so we can return this to js data['comment_list'] = [] data['cor_spots'] = [] for cl in comment_list: data['comment_list'].append(cl.toJsonFormat()) cor_spot = cl.spot_set.all() # get the first one, in fact there is only one, but the return is a struct of arrary cor_spot_ = None for cors in cor_spot: cor_spot_ = cors data['cor_spots'].append(cor_spot_.toJsonFormat(show_comment=True)) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def user(request): ''' 获取个人信息,必须登陆后才行 参数: 返回值: 没有任何异常,则 {'status': 1, 'info': 'ok', 'data': 用户的信息} 否则 {‘status': 0, 'info': 'error'} ''' data = {'status': 0, 'info': 'error'} try: authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads(auth.data) # this is the json format User, not a class type User user_ = User.objects.get(id=cur_user['id']) data['data'] = user_.toJsonFormat() data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def get_user_message(request): data = {'status': 0, 'info': 'error'} try: authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads(auth.data) # this is the json format User, not a class type User user_ = User.objects.get(id=cur_user['id']) unread_messages = Message.objects.filter(to_user=user_, is_read=False) unread_messages_count = unread_messages.count() data['unread_messages_count'] = unread_messages_count data['unread_messages'] = [] for urm in unread_messages: data['unread_messages'].append(urm.toJsonFormat()) read_messages = Message.objects.filter(to_user=user_, is_read=True) read_messages_count = read_messages.count() data['read_messages_count'] = read_messages_count data['read_messages'] = [] for rm in read_messages: data['read_messages'].append(rm.toJsonFormat()) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def home_ownComment(request): data = {'status': 0, 'info': 'error'} try: # get current user authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads( auth.data ) # this return "an array" with the properties in an User object. user_ = User.objects.get(id=cur_user['id']) # the comment need to corespond to a spot, so we need to find out the comment and spot together comment_list = Comment.objects.filter(creator=user_) comment_count = Comment.objects.filter(creator=user_).count() data['comment_count'] = comment_count # as the comment_list and cor_spots in the correct coresponding order, so we can return this to js data['comment_list'] = [] data['cor_spots'] = [] for cl in comment_list: data['comment_list'].append(cl.toJsonFormat()) cor_spot = cl.spot_set.all( ) # get the first one, in fact there is only one, but the return is a struct of arrary cor_spot_ = None for cors in cor_spot: cor_spot_ = cors data['cor_spots'].append(cor_spot_.toJsonFormat(show_comment=True)) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def topic_add(request): data = {'status': 0, 'info': 'error'} try: topic = Topic() except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def user_update(request): data = {'status': 0, 'info': 'error'} try: data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def createSplitslot(request): data = {'status': 0, 'info': 'error'} try: # 获取当前用户 authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads( auth.data) # this is the json format User, not a class type User user_ = User.objects.get(id=cur_user['id']) # 获取createSplitlot.js传过来的数据 title = request.REQUEST['title'] content = request.REQUEST['content'] topicID = request.REQUEST['topicID'] createTime = request.REQUEST['create_time'] # 创建新的spot,但未关联对应的images new_spot = Spot(creator=user_, create_time=createTime, title=title, content=content, up=0) new_spot.save() image_strings = request.REQUEST['imgs'] # 解码images并保存到数据库,及关联到new_spot imageList = image_strings.split(';') for il in imageList: img_name = ''.join([random.choice(string.ascii_letters + string.digits) \ for i in range(15)]) img_url = 'resource/images/' + img_name + '.jpeg' imgData = base64.b64decode(il) tmp = open(img_url, 'wb') tmp.write(imgData) tmp.close() image = Image(url=img_url) image.save() print image.url new_spot.imgs.add(image) # 关联new_spot与对应的topic new_spot.save() topic = Topic.objects.get(id=topicID) topic.spots.add(new_spot) topic.save() data['spotID'] = new_spot.id data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def get_message_detail(request): data = {'status': 0, 'info': 'error'} try: messageID = request.REQUEST['messageID'] message = Message.objects.get(id=messageID) message.is_read = True message.save() data['message'] = message.toJsonFormat() data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def createSplitslot(request): data ={'status': 0, 'info': 'error'} try: # 获取当前用户 authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads(auth.data) # this is the json format User, not a class type User user_ = User.objects.get(id=cur_user['id']) # 获取createSplitlot.js传过来的数据 title = request.REQUEST['title'] content = request.REQUEST['content'] topicID = request.REQUEST['topicID'] createTime = request.REQUEST['create_time'] # 创建新的spot,但未关联对应的images new_spot = Spot(creator=user_, create_time=createTime, title=title, content=content, up=0) new_spot.save() image_strings = request.REQUEST['imgs'] # 解码images并保存到数据库,及关联到new_spot imageList = image_strings.split(';') for il in imageList: img_name = ''.join([random.choice(string.ascii_letters + string.digits) \ for i in range(15)]) img_url = 'resource/images/' + img_name + '.jpeg' imgData = base64.b64decode(il) tmp = open(img_url,'wb') tmp.write(imgData) tmp.close() image = Image(url=img_url) image.save() print image.url new_spot.imgs.add(image) # 关联new_spot与对应的topic new_spot.save() topic = Topic.objects.get(id=topicID) topic.spots.add(new_spot) topic.save() data['spotID'] = new_spot.id data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def logout(request): ''' 登出 ''' data = {'status': 0, 'info': 'error'} try: authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) auth.delete() data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def add_up(request): ''' add up of a spot ''' data = {'status': 0, 'info': 'error'} try: spot_id = int(request.REQUEST['id']) spot_ = Spot.objects.get(id=spot_id) spot_.up = int(spot_.up) + 1 spot_.save() data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def get_Topic(request): data = {'status': 0, 'info': 'error'} try: spotID = request.REQUEST['spitlotID'] spot_ = Spot.objects.get(id=spotID) topicList = spot_.topic_set.all() # acttrually only one topicID = -1 for tl in topicList: topicID = tl.id data['topicID'] = topicID data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def register(request): ''' 注册新用户 参数: request.REQUEST['username']: 账号 request.REQUEST['password']: 密码 返回值: 如果成功,则返回 {'status': 1, 'info': 'ok', 否则 {'status': 0, 'info': 'error'} 其他: ''' data = {'status': 0, 'info': 'error'} try: user = User() user.username = request.REQUEST['username'] user.password = request.REQUEST['password'] user.icon = Image.objects.get(id=1) #设置默认头像 if user.username.strip() == '' or user.password.strip() == '': raise # 账号或者密码为空 user.save()# 插入数据 user_data = user.toJsonFormat() auth = Auth() auth.key = util.generate_random_string(32) auth.data = json.dumps(user_data) auth.save() create_time = request.REQUEST['create_time'] # system send a welcome message to user system_user = User.objects.get(username="******") welcome = Message(to_user=user, from_user=system_user, is_read=False, content="Welcome to WeTalk!", create_time=create_time) welcome.save() data['authkey'] = auth.key data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def search_topic(request): data = {'status': 0, 'info': 'error'} try: title_ = request.REQUEST['title'] # match the topic title contain key "title" from request # "PorpertyName__contains" is a way offer from Django topic_list = Topic.objects.filter(title__contains=title_) topic_count = topic_list.count() data['topic_count'] = topic_count data['topic_list'] = [] for tl in topic_list: data['topic_list'].append(tl.toJsonFormat()) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def user_update(request): data = {'status': 0, 'info': 'error'} try: authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads(auth.data) # this is the json format User, not a class type User user_ = User.objects.get(id=cur_user['id']) infoType = request.REQUEST['infoType'] infoText = request.REQUEST['infoText'] if infoType == '0': user_.name = infoText if infoType == '1': user_.password = infoText if infoType == '2': user_.intro = infoText if infoType == '3': user_.interest = infoText if infoType == '4': img_name = ''.join([random.choice(string.ascii_letters + string.digits) \ for i in range(15)]) img_url = 'resource/images/' + img_name + '.jpeg' #pic = cStringIO.StringIO() #image_string = cStringIO.StringIO(base64.b64decode(infoText)) # 解码images并保存到数据库,及关联到user_ imgData = base64.b64decode(infoText) tmp = open(img_url,'wb') tmp.write(imgData) tmp.close() image = Image(url=img_url) image.save() user_.icon = image user_.save() data['user'] = user_.toJsonFormat() data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def userFocus_topic(request): data = {'status': 0, 'info': 'error'} try: authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads(auth.data) # this return "an array" with the properties in an User object. user_ = User.objects.get(id=cur_user['id']) fTopic_list_ = user_.focus.all() fTopic_count_ = user_.focus.count() data['fTopic_count'] = fTopic_count_ data['fTopic_list'] = [] for ft in fTopic_list_: data['fTopic_list'].append(ft.toJsonFormat()); data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def spot_list(request): ''' 根据话题id获取该话题的所有槽点 参数: request.REQUEST['id']: 话题的id request.REQUEST['start']: 区间的开始值 request.REQUEST['end']: 区间的结束值 返回值: 如果成功,则 {'status': 1, 'info': 'ok', 'count': 该话题的总槽点个数, 'data': [槽点们]} 否则 {'status': 0, 'info': 'error'} 其他: 区间为[start, end) ''' data = {'status': 0, 'info': 'error'} try: topic_id = int(request.REQUEST['id']) start = int(request.REQUEST['start']) end = int(request.REQUEST['end']) topic = Topic.objects.get(id=topic_id) data['count'] = topic.spots.count() sp_list = Topic.objects.get(id=topic_id).spots.all()[start:end] data['data'] = [] for sp in sp_list: data['data'].append(sp.toJsonFormat()) data['topic'] = topic.toJsonFormat() data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def userFocus_topic(request): data = {'status': 0, 'info': 'error'} try: authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads( auth.data ) # this return "an array" with the properties in an User object. user_ = User.objects.get(id=cur_user['id']) fTopic_list_ = user_.focus.all() fTopic_count_ = user_.focus.count() data['fTopic_count'] = fTopic_count_ data['fTopic_list'] = [] for ft in fTopic_list_: data['fTopic_list'].append(ft.toJsonFormat()) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def register(request): ''' 注册新用户 参数: request.REQUEST['username']: 账号 request.REQUEST['password']: 密码 返回值: 如果成功,则返回 {'status': 1, 'info': 'ok', 否则 {'status': 0, 'info': 'error'} 其他: ''' data = {'status': 0, 'info': 'error'} try: user = User() user.username = request.REQUEST['username'] user.password = request.REQUEST['password'] user.icon = Image.objects.get(id=1) #设置默认头像 if user.username.strip() == '' or user.password.strip() == '': raise # 账号或者密码为空 user.save() # 插入数据 user_data = user.toJsonFormat() auth = Auth() auth.key = util.generate_random_string(32) auth.data = json.dumps(user_data) auth.save() data['authkey'] = auth.key data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def register(request): ''' 注册新用户 参数: request.REQUEST['username']: 账号 request.REQUEST['password']: 密码 返回值: 如果成功,则返回 {'status': 1, 'info': 'ok', 否则 {'status': 0, 'info': 'error'} 其他: ''' data = {'status': 0, 'info': 'error'} try: user = User() user.username = request.REQUEST['username'] user.password = request.REQUEST['password'] user.icon = Image.objects.get(id=1)#设置默认头像 if user.username.strip() == '' or user.password.strip() == '': raise # 账号或者密码为空 user.save()# 插入数据 user_data = user.toJsonFormat() auth = Auth() auth.key = util.generate_random_string(32) auth.data = json.dumps(user_data) auth.save() data['authkey'] = auth.key data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def topic_list(request): ''' 获取话题 参数: request.REQUEST['category']: 类型的下标 request.REQUEST['start']: 区间的开始值 request.REQUEST['end']: 区间的结束值 返回值: 如果成功,则 {'status': 1, 'info': 'ok', 'count': 该类型的总话题个数 'data': [话题们]} 否则 {'status': 0, 'info': 'error'} 其他: 区间为[start, end) ''' data = {'status': 0, 'info': 'error'} try: cg = categories[int(request.REQUEST['category'])] start = int(request.REQUEST['start']) end = int(request.REQUEST['end']) #data['count'] = Topic.objects.filter(category=cg).count() tp_list = Topic.objects.filter(category=cg).order_by('id')[start:end] data['count'] = tp_list.count() data['data'] = [] for tp in tp_list: data['data'].append(tp.toJsonFormat()) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def comment_list(request): ''' 获取某槽点及其评论 参数: request.REQUEST['id']: 槽点id request.REQUEST['start']: 区间的开始值 request.REQUEST['end']: 区间的结束值 返回值: 如果成功,则 {'status': 1, 'info': 'ok', 'data': 数据} 否则 {'status': 0, 'info': 'error'} 其他: 区间为左闭右开,即[start, end) ''' data = {'status': 0, 'info': 'error'} try: spot_id = int(request.REQUEST['id']) start = int(request.REQUEST['start']) end = int(request.REQUEST['end']) #spot_id = 1 #start = 0 #end = 1 sp = Spot.objects.get(id=spot_id) data['data'] = sp.toJsonFormat(show_comment=True, start=start, end=end) data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def image_upload(request): data = {'status': 0, 'info': 'error'} try: size = 200, 200 img_name = ''.join([random.choice(string.ascii_letters + string.digits) \ for i in range(15)]) upload_img = request.FILES['image'] img = PILImage.open(upload_img) img.thumbnail(size, PILImage.ANTIALIAS) img_url = 'resource/images/' + img_name + '.png' img.save(img_url, 'png') image = Image() image.url = img_url image.save() data['status'] = 1 data['info'] = 'ok' data['url'] = img_url except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def login(request): ''' 登陆系统 参数: request.REQUEST['username']: 账号 request.REQUEST['password']: 密码 返回值: 如果成功,则返回 {'status': 1, 'info': 'ok', 否则 {'status': 0, 'info': 'error'} 其他: ''' data = {'status': 0, 'info': 'error'} try: username = request.REQUEST['username'] password = request.REQUEST['password'] user = User.objects.get(username=username, password=password) user_data = user.toJsonFormat() auth = Auth() auth.key = util.generate_random_string(32) auth.data = json.dumps(user_data) auth.save() data['authkey'] = auth.key data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))
def createTopic(request): data = {'status': 0, 'info': 'error'} try: authkey = request.REQUEST['authkey'] auth = Auth.objects.get(key=authkey) cur_user = json.loads(auth.data) # this is the json format User, not a class type User user_ = User.objects.get(id=cur_user['id']) create_time = request.REQUEST['create_time'] title = request.REQUEST['title'] begin_time = request.REQUEST['startTime'] end_time = request.REQUEST['endTime'] category = request.REQUEST['category'] new_topic = Topic(creator=user_, create_time=create_time, category=category, title=title, begin_time=begin_time, end_time=end_time) new_topic.save() data['topicID'] = new_topic.id data['status'] = 1 data['info'] = 'ok' except Exception as e: data['info'] = util.get_exception_message(e) print e return HttpResponse(json.dumps(data))