def post(self, request): data = json.loads(request.body) phone = data["phone"] password = data["password"] users = User.objects.filter(phone=phone).all() if len(users) > 0: return to_json_data(errno=1, errmsg="该手机已被注册") else: try: User.objects.create(name="用户{}".format(phone), phone=phone, password=password, introduce="写一段个人介绍,让大家认识一下吧!", address="保密", birth="2000.01.01") user = User.objects.get(phone=phone) data = { "name": user.name, "gender": user.gender, "address": user.address, "birth": user.birth, "introduce": user.introduce, "imgUrl": user.imgUrl, "phone": user.phone, "fansNum": 0, "focusNum": 0, "trendNum": 0 } return to_json_data(errno=0, errmsg="注册成功", data=data) except Exception as e: print(e) return to_json_data(errno=1, errmsg="未知错误")
def post(self, request): data = json.loads(request.body) phone = data["phone"] password = data["password"] users = User.objects.filter(phone=phone).all() if len(users) == 0: return to_json_data(errno=1, errmsg="用户不存在") else: try: user = User.objects.get(phone=phone) if user.password != password: return to_json_data(errno=1, errmsg="密码错误") data = { "name": user.name, "gender": user.gender, "address": user.address, "birth": user.birth, "introduce": user.introduce, "imgUrl": user.imgUrl, "phone": user.phone, "fansNum": 0, "focusNum": 0, "trendNum": 0 } return to_json_data(errno=0, errmsg="登录成功", data=data) except Exception as e: print(e) return to_json_data(errno=1, errmsg="未知错误")
def get(self, request): trends = Trends.objects.all() dataList = [] for trend in trends: # 获取配图列表 imageList = [] images = TrendsImage.objects.filter(trends=trend).all() for image in images: imageList.append(image.url) # 获取评论列表 comments = TrendsComment.objects.filter( trends=trend).order_by("date").all() # 添加进list dataList.append({ "id": trend.id, "userName": trend.user.name, "likeCounts": trend.likeCounts, "userImage": trend.user.imgUrl, "content": trend.content, "pubDate": trend.pubDate.strftime("%Y.%m.%d %H:%M"), "tag": trend.tag.name, "images": imageList, "commentsCount": comments.count() }) return to_json_data(errno=0, errmsg="OK", data=dataList)
def get(self, request): try: id = request.GET["id"] print(id) note = TravelNote.objects.get(id=id) # 标签 tag_list = [] tags = note.tag.all() print(tags) for t in tags: tag_list.append(t.name) # 配图 img_list = [] images = TravelNoteImage.objects.filter(travel_note=note) for i in images: img_list.append(i.url) data = { "travel_note": { "id": note.id, "title": note.title, "address": note.address, "userName": note.user.name, "userImage": note.user.imgUrl, "likeCounts": note.likeCounts, "content": note.content, "pubDate": note.pubDate.strftime("%Y/%m/%d") }, "images": img_list, "tags": tag_list, } return to_json_data(errno=Code.OK, errmsg=error_map[Code.OK], data=data) except Exception as e: pass
def get(self, request): help_id = request.GET["id"] try: help = HelpInfo.objects.get(id=help_id) # 获取配图列表 imageList = [] images = HelpInfoImage.objects.filter(help=help).all() for image in images: imageList.append(image.url) # 获取评论列表 commentList = [] comments = HelpComment.objects.filter( helpInfo=help).order_by("date").all() for comment in comments: imgs = HelpCommnetImage.objects.filter( helpComment=comment).all() imgList = [] for img in imgs: imgList.append(img.url) commentList.append({ "userName": comment.user.name, "userImage": comment.user.imgUrl, "content": comment.content, "pubDate": comment.date.strftime("%Y.%m.%d %H:%M"), "images": imgList, "likeCounts": comment.likeCounts }) data = { "userName": help.user.name, "userImage": help.user.imgUrl, "content": help.content, "pubDate": help.pubDate.strftime("%Y.%m.%d %H:%M"), "tag": help.tag, "images": imageList, "comments": commentList } return to_json_data(errno=1, errmsg="None Data", data=data) except Exception as e: print(e) return to_json_data(errno=1, errmsg="None Data")
def get(self, request): trend_id = request.GET["id"] try: trend = Trends.objects.get(id=trend_id) # 获取配图列表 imageList = [] images = TrendsImage.objects.filter(trends=trend).all() for image in images: imageList.append(image.url) # 获取评论列表 commentList = [] comments = TrendsComment.objects.filter( trends=trend).order_by("date").all() for comment in comments: imgs = TrendsCommentImage.objects.filter(comment=comment).all() imgList = [] for img in imgs: imgList.append(img.url) commentList.append({ "userName": comment.user.name, "userImage": comment.user.imgUrl, "content": comment.content, "pubDate": comment.date.strftime("%Y.%m.%d %H:%M"), "images": imgList, "likeCounts": comment.likeCounts }) data = { "likeCounts": trend.likeCounts, "userName": trend.user.name, "userImage": trend.user.imgUrl, "content": trend.content, "pubDate": trend.pubDate.strftime("%Y.%m.%d %H:%M"), "tag": trend.tag.name, "images": imageList, "comments": commentList } return to_json_data(errno=1, errmsg="None Data", data=data) except Exception as e: return to_json_data(errno=1, errmsg="None Data")
def post(self, request): phone = request.POST.get("phone") content = request.POST.get("content") image = request.FILES.get("image") user = User.objects.get(phone=phone) try: # Diary.objects.create(user=user, image=image, content=content) # diary = Diary.objects.get(user__phone=phone) diary = Diary(user=user, image=image, content=content) diary.save() res = { "id": diary.id, "content": diary.content, "image": "http://47.93.130.219:8000" + diary.image.url, "pubDate": diary.date.strftime("%Y/%m/%d"), "userName": diary.user.name, "userImage": diary.user.imgUrl } return to_json_data(errno=0, errmsg="OK", data=res) except Exception as e: print(e) to_json_data(errno=1, errmsg="发布失败")
def get(self, request): cultureList = Culture.objects.all() dataList = [] for i in cultureList: dataList.append({ "id":i.id, "title":i.title, "likeCounts":i.likeCounts, "image":i.image, "type":i.type.name, "url":i.url }) return to_json_data(errno=0, errmsg="OK", data=dataList)
def get(self, request): id = request.GET["id"] # TODO: 用户 -- 是否喜欢 food = Food.objects.filter(id=id).first() food_images = FoodImage.objects.filter(food=food).all() image_list = [] for i in food_images: image_list.append(i.url) restaurants = food.recommendedRestaurant.all() restaurant_list = [] for r in restaurants: img = r.restaurantimage_set.first() tag = RestaurantTag.objects.filter(restaurant=r).first() if not tag: tag = None else: tag = tag.tag restaurant_list.append({ "id": r.id, "name": r.name, "score": r.commentCount.score, "commentNumber": r.commentCount.commentNumber, "tag": tag, "imgUrl": img.url, "position": r.position, "averageConsumption": r.averageConsumption }) data = { "id": food.id, "name": food.name, "description": food.description, "story": food.story, "feature": food.feature, "practice": food.practice, "value": food.value, "likeCounts": food.likeCounts, "tag": food.tag, "imgs": image_list, "islike": True, "restaurants": restaurant_list, } return to_json_data(errno=Code.OK, errmsg=error_map[Code.OK], data=data)
def get(self, request): food_list = [] foods = Food.objects.all() for f in foods: f_image = FoodImage.objects.values("url").filter(food=f).first() food_list.append({ "id": f.id, "name": f.name, "description": f.description, "image": f_image["url"], "restaurant_counts": f.recommendedRestaurant.count() }) return to_json_data(errno=Code.OK, errmsg=error_map[Code.OK], data=food_list)
def get(self, request): try: phone = request.GET["phone"] except Exception as e: phone = None if phone: list = Diary.objects.filter(user__phone=phone).all() else: list = Diary.objects.all() dataList = [] for i in list: dataList.append({ "id": i.id, "content": i.content, "image": "http://47.93.130.219:8000" + i.image.url, "userName": i.user.name, "likeCount": i.likeCount, "userImage": i.user.imgUrl, "pubDate": i.date.strftime("%Y/%m/%d") }) return to_json_data(errno=0, errmsg="OK", data=dataList)
def get(self, request): note_list = [] notes = TravelNote.objects.all() for r in notes: tag = r.tag.first() if not tag: tag = None else: tag = tag.name note_list.append({ "id": r.id, "title": r.title, "tag": tag, "likeCounts": r.likeCounts, "image": r.travelnoteimage_set.first().url, "userName": r.user.name, "userImage": r.user.imgUrl, "pubDate": r.pubDate.strftime("%Y.%m.%d") }) return to_json_data(errno=Code.OK, errmsg=error_map[Code.OK], data=note_list)
def get(self, request): restaurant_list = [] restaurants = Restaurant.objects.all() for r in restaurants: tag = r.restauranttag_set.first() if not tag: tag = None else: tag = tag.tag restaurant_list.append({ "id": r.id, "name": r.name, "score": r.commentCount.score, "commentNumber": r.commentCount.commentNumber, "tag": tag, "imgUrl": r.restaurantimage_set.first().url, "position": r.position, "averageConsumption": r.averageConsumption }) return to_json_data(errno=Code.OK, errmsg=error_map[Code.OK], data=restaurant_list)
def get(self, request): helps = HelpInfo.objects.all() dataList = [] for help in helps: # 获取配图列表 imageList = [] images = HelpInfoImage.objects.filter(help=help).all() for image in images: imageList.append(image.url) # 获取评论数量 comments = HelpComment.objects.filter(helpInfo=help).all() # 添加进list dataList.append({ "id": help.id, "userName": help.user.name, "userImage": help.user.imgUrl, "content": help.content, "pubDate": help.pubDate.strftime("%Y.%m.%d %H:%M"), "tag": help.tag, "images": imageList, "commentsCount": comments.count() }) return to_json_data(errno=0, errmsg="OK", data=dataList)
def get(self, request): t_a_list = [] tourist_attractions = TouristAttraction.objects.all() for t in tourist_attractions: image = t.touristattractionimage_set.values("url").first() tag = t.touristattractiontag_set.values("tag").first() if tag == None: tag = None else: tag = tag["tag"] t_a_list.append({ "id": t.id, "name": t.name, "position": t.position, "tag": tag, "averageConsumption": t.averageConsumption, "score": t.commentCount.score, "commentNumber": t.commentCount.commentNumber, "image": image["url"], }) return to_json_data(errno=Code.OK, errmsg=error_map[Code.OK], data=t_a_list)
def get(self, request): id = request.GET["id"] print(id) # 当前景区 t_s = TouristSpot.objects.get(id=id) t_s_info = { "id": t_s.id, "name": t_s.name, "position": t_s.position, "description": t_s.description, "openDate": t_s.openDate.open_date, "openTime": t_s.openDate.open_time, "history": t_s.history, "artistCharm": t_s.artistCharm, "feature": t_s.feature } # 景点配图 t_s_images = TouristSpotImage.objects.filter(touristSpot=t_s) image_list = [] for i in t_s_images: image_list.append(i.url) t_s_tags = TouristSpotTag.objects.filter(touristSpot=t_s) tag_list = [] for i in t_s_tags: tag_list.append(i.tag) data = { "tourist_spot": t_s_info, "images": image_list, "tourist_tag": tag_list } return to_json_data(errno=Code.OK, errmsg=error_map[Code.OK], data=data)
def get(self, request): id = request.GET["id"] # 当前景区 t_a = TouristAttraction.objects.get(id=id) # TODO:用户收藏状态 t_a_info = { "id": t_a.id, "name": t_a.name, "position": t_a.position, "averageConsumption": t_a.averageConsumption, "description": t_a.description, "openDate": t_a.openDate.open_date, "openTime": t_a.openDate.open_time, "ticketInfo": t_a.ticketInfo, "useTime": t_a.useTime, "bestTime": t_a.bestTimes, "tips": t_a.bestTimes, "busInfo": t_a.busInfo, "score": t_a.commentCount.score, "commentNumber": t_a.commentCount.commentNumber, "positiveCommentNumber": t_a.commentCount.positiveCommentNumber, "negativeCommentNumber": t_a.commentCount.negativeCommentNumber, "isLike": False, } # 景区配图 t_a_images = TouristAttractionImage.objects.filter( touristAttraction=t_a) image_list = [] for i in t_a_images: image_list.append(i.url) # 评论 t_a_comments = TouristAttractionComment.objects.filter( touristAttraction=t_a) if len(t_a_comments) > 2: t_a_comments = t_a_comments[0:2] comment_list = [] for i in t_a_comments: imgs = [] for j in i.touristattractioncommentimage_set.values("url"): imgs.append(j["url"]) comment_list.append({ "id": i.id, "user_id": i.user_id, "user_name": i.user.name, "user_img": i.user.imgUrl, "content": i.content, "imgs": imgs, "stars": i.stars, "isGood": i.manner, "isQuality": i.isQuality, "publicTime": i.public_time.strftime("%Y.%m.%d") }) t_a_tags = TouristAttractionTag.objects.filter(touristAttraction=t_a) tag_list = [] for i in t_a_tags: tag_list.append(i.tag) # 景点 t_a_spots = TouristSpot.objects.filter(touristAttraction=t_a) tourist_spot_list = [] for i in t_a_spots: img = i.touristspotimage_set.values("url").first() if not img: img = None else: img = img["url"] tourist_spot_list.append({ "id": i.id, "name": i.name, "image": img }) data = { "tourist_attraction": t_a_info, "images": image_list, "comments": comment_list, "tourist_spot": tourist_spot_list, "tourist_tag": tag_list } return to_json_data(errno=Code.OK, errmsg=error_map[Code.OK], data=data)
def get(self, request): try: id = request.GET["id"] restaurant = Restaurant.objects.get(id=id) # 评论 restaurant_comments = RestaurantComment.objects.filter( restaurant=restaurant) if len(restaurant_comments) > 2: restaurant_comments = restaurant_comments[0:2] comment_list = [] for c in restaurant_comments: imgs = [] for i in RestaurantCommentImage.objects.filter( restaurantComment=c): imgs.append(i.url) comment_list.append({ "id": c.id, "content": c.content, "user_id": c.user.id, "user_name": c.user.name, "user_img": c.user.imgUrl, "images": imgs, "stars": c.stars, "isGood": c.manner, "isQuality": c.isQuality, "publicTime": c.public_time.strftime("%Y.%m.%d") }) # 标签 tag_list = [] restaurant_tags = RestaurantTag.objects.filter( restaurant=restaurant) for t in restaurant_tags: tag_list.append(t.tag) # 配图 img_list = [] images = RestaurantImage.objects.filter(restaurant=restaurant) for i in images: img_list.append(i.url) # 菜品 food_list = [] restaurant_foods = RestaurantFood.objects.filter( restaurant=restaurant) for f in restaurant_foods: food_list.append(f.name) data = { "restaurant": { "id": restaurant.id, "name": restaurant.name, "position": restaurant.position, "averageConsumption": restaurant.averageConsumption, "description": restaurant.description, "phone": restaurant.phone, "openDate": restaurant.openDate.open_date, "openTime": restaurant.openDate.open_time, "score": restaurant.commentCount.score, "isCollect": True, "commentNumber": restaurant.commentCount.commentNumber, "positiveCommentNumber": restaurant.commentCount.positiveCommentNumber, "negativeCommentNumber": restaurant.commentCount.negativeCommentNumber }, "comments": comment_list, "images": img_list, "tags": tag_list, "foods": food_list } return to_json_data(errno=Code.OK, errmsg=error_map[Code.OK], data=data) except Exception as e: pass