def add_review_views(request, aid): if request.session.get('username'): login_uname = request.session.get('username') user = User.objects.get(username=login_uname) a = Article.objects.get(id=aid) user1 = User.objects.get(id=a.user_id) if request.method == 'POST': # 判断文章是否可被评论 if (a.is_reviewed == "1"): rcontent = request.POST.get('content') Review.objects.create(content=rcontent, article_id=aid, user_id=user.id) a.reviewed_num = a.reviewed_num + 1 a.save() messages.success(request, '评论发布成功!') n = Notification(aim_user=user1, arg0=5, arg1=aid, arg4=user) n.save() return HttpResponseRedirect('/read_article/' + str(a.id)) else: messages.error(request, '文章不可被评论!') return HttpResponseRedirect('/read_article/' + str(a.id)) else: return render(request, 'add_review.html', locals()) else: messages.error(request, '请登录!') return HttpResponseRedirect('/')
def admire_goodnum_views(request): if request.method == 'POST': uid = request.POST.get('userID') good_content = request.POST.get('goodINPUT') good_id = request.POST.get('goodID') #文件的id admireType = request.POST.get('admireType') isAdd = request.POST.get('isAdd') user = User.objects.get(username=request.session['username']) user1=User.objects.get(id=file.user_id) if admireType == 'file': file=File.objects.get(id=good_id) File.objects.filter(id=good_id).update(file_beadmired = good_content) if isAdd=='1': Admirelog.objects.create(uid_id=uid,fid_id=good_id,isGood=True,isFile=True) #,fid_id=-1 n = Notification(aim_user=user1, arg0=6, arg4=user) n.save() else: Admirelog.objects.get(isGood = True,uid_id=uid,fid_id=good_id).delete() #,fid_id=-1 return HttpResponseRedirect('/share') else: article=Article.objects.get(id=good_id) Article.objects.filter(id=good_id).update(beadmired_num=good_content) if isAdd=='1': n = Notification(aim_user=user1, arg0=8,arg1=good_id, arg4=user) n.save() Admirelog.objects.create(uid_id=uid,aid_id=good_id,isGood=True,isFile=False) #,fid_id=-1 # isadd = '0' 代表取消点赞 所以应该将 isGood为1 的删除 else: Admirelog.objects.get(isGood = True,uid_id=uid,aid_id=good_id).delete() #,fid_id=-1 return HttpResponse("aa")
def admire_badnum_views(request): if request.method == 'POST': uid = request.POST.get('userID') bad_content = request.POST.get('badINPUT') bad_id = request.POST.get('badID') admireType = request.POST.get('admireType') isAdd = request.POST.get('isAdd') user = User.objects.get(username=request.session['username']) user1=User.objects.get(id=file.user_id) # 文件 if admireType == 'file': file=File.objects.get(id=bad_id) File.objects.filter(id=bad_id).update(file_benotadmired = bad_content) if isAdd=='1': Admirelog.objects.create(uid_id=uid,fid_id=bad_id,isGood=False,isFile=True)#,fid_id=-1 n = Notification(aim_user=user1, arg0=4,arg4=user) n.save() else: Admirelog.objects.get(isGood=False,uid_id=uid,fid_id=bad_id).delete()#,fid_id=-1 return HttpResponseRedirect('/share') # 文章 else: article=Article.objects.get(id=bad_id) Article.objects.filter(id=bad_id).update(benotadmired_num=bad_content) if isAdd=='1': Admirelog.objects.create(uid_id=uid,aid_id=bad_id,isGood=False,isFile=False)#,fid_id=-1 n = Notification(aim_user=user1, arg0=7,arg1=good_id,arg4=user) n.save() else: Admirelog.objects.get(isGood=False,uid_id=uid,aid_id=bad_id).delete()#,fid_id=-1 return
def new_order_views(request, good_id): if request.method == 'POST': good = Good.objects.get(id=good_id) confirm = request.POST.get('confirm-buy', '') if confirm == '': messages.warning(request, '你未确认交易信息') return render(request, 'ordering.html', locals()) else: good = Good.objects.get(id=good_id) user = User.objects.get(username=request.session['username']) order = Order.objects.filter(good_id=good_id).filter( creator=user.id) print(order) if good.isfile == True or len(order) == 0: if good.creator == user: messages.warning(request, '不允许购买自己发布的商品') return render(request, 'ordering.html', locals()) new_order = Order(status=0, creator=user, good=good) new_order.save() st = good.sell_times + 1 good.sell_times = st good.save() n = Notification(aim_user=good.creator, arg0=1, arg1=new_order.id, arg4=user) n.save() return redirect(reverse('paying', args=(new_order.id, ))) else: return redirect(reverse('paying', args=(order, )))
def buyer_ok_views(request, order_id): if request.method == 'POST': user = User.objects.get(username=request.session['username']) order = Order.objects.get(id=order_id) if user != order.creator: messages.error(request, '没有权限') return render(request, '/', locals()) order.buyer_ok = True order.status = 1 order.save() #消息处理 good = order.good buyer = order.creator seller = good.creator buyer_message = request.POST.get('buyer-ok-message', '') if not buyer_message == '': message = TradeMessage(sender=user, receiver=seller, order=order, content=buyer_message) n = Notification(aim_user=seller, arg0=2, arg1=order_id, arg4=user) message.save() n.save() return redirect(reverse('paying', args=(order.id, )))
def post(self, request, post_id): if not Post.objects.filter(pk=post_id): return Response(False) serializer = PostSerializer(data=request.data) if serializer.is_valid(): post = Post.objects.get(pk=post_id) post.title = serializer.data['title'] post.body = serializer.data['body'] post.postType = serializer.data['postType'] post.category = serializer.data['category'] post.edited = True post.save() for user in Postcomment.objects.filter(targetpost=post): newNote = Notification(user=user.author, message=request.user.username + ' edited a post you are following', url='/post/' + str(post.pk), img='create.png') newNote.save() alert = user.author alert.numofnewnote += 1 alert.save() return Response(serializer.data) else: return Response(False)
def get(self, request, username): if not User.objects.filter(username=username): return Response('The member you want to follow is invalid.') if request.user.username == username: # This is to ensure a user can't like his/her own page return Response("Sorry, you can't like you own page ") else: user = User.objects.get(username=username) exitence = Likepage.objects.filter(user=user, fan=request.user.username) # The above decision is to ensure you can't a page more than once. if exitence: return Response('You have liked this page before') else: likepage = Likepage(user=user, fan=request.user.username) likepage.save() # Creating Notification for new page liking newNote = Notification(user=user, message=request.user.username + ' like your page', url='/profile/' + request.user.username, img='home.png') newNote.save() # alert alert = newNote.user alert.numofnewnote += 1 alert.save() return Response("You liked " + username + "'s page")
def post(self, request): serializer = AddPostSerializer(data=request.data) if serializer.is_valid(): # logged in user user = User.objects.get(username=request.user.username) serializer.save() newData = serializer.data post = Post.objects.get(pk=serializer.data['id']) newData['background'] = post.background = random.choice(postpics) post.save() newData['date'] = post.date following = [] for followMem in user.following_set.all(): following.append(followMem.follow) for follower in Following.objects.filter(follow=user): newNote = Notification( user=User.objects.get(username=follower.member), message=user.username + ' created a post on ' + serializer.data['postType'] + 's', url='/post/' + str(serializer.data['id']), img='create.png') newNote.save() alert = newNote.user alert.numofnewnote += 1 alert.save() return Response(newData) else: return Response(False)
def add_tmessage_views(request, order_id): if request.method == 'POST': content = request.POST.get('content', '') order = Order.objects.get(id=order_id) good = order.good user = User.objects.get(username=request.session['username']) buyer = order.creator seller = good.creator if not content == '': if user.id == buyer.id: message = TradeMessage(sender=user, receiver=seller, order=order, content=content) try: n = Notification.objects.get(Q(arg0=0)&Q(arg1=order_id)&Q(aim_user=seller)) n.arg2 += 1 n.have_read = False except: n = Notification(aim_user=seller, arg0=0, arg1=order_id, arg2=1, arg3=0, arg4=user) else: message = TradeMessage(sender=user, receiver=buyer, order=order, content=content) try: n = Notification.objects.get(Q(arg0=0)&Q(arg1=order_id)&Q(aim_user=buyer)) n.arg2 += 1 n.have_read = False except: n = Notification(aim_user=buyer, arg0=0, arg1=order_id, arg2=1, arg3=1, arg4=user) #arg0:0聊天信息 1交易信息 2买家已付款 3卖家已确认 #arg1:订单id #arg2:暂存消息数量吧 #arg3:0由买家发来 1由卖家发来 #arg4:用户外键 message.save() n.save() return render(request, 'paying.html', locals())
def seller_ok_views(request, order_id): if request.method == 'POST': user = User.objects.get(username=request.session['username']) order = Order.objects.get(id=order_id) if user != order.good.creator: messages.error(request, '没有权限') return render(request, '/', locals()) order.buyer_ok = True order.status = 2 order.save() good = order.good good.sell_times += 1 good.save() buyer = order.creator buyer.trade_count += 1 buyer.save() seller = good.creator seller.trade_count += 1 seller.save() #消息处理 n = Notification(aim_user=buyer, arg0=3, arg1=order_id, arg4=user) n.save() return redirect(reverse('paying', args=(order.id, )))
def get(self, request, action, username): if action == 'follow': if Following.objects.filter( member=request.user.username, follow=User.objects.get(username=username)): return Response(True) else: if username == request.user.username: return Response(False) else: fol = Following(member=request.user.username, follow=User.objects.get(username=username)) fol.save() # Creating a notification for new followship(The person being followed receive note) newNotification = Notification( user=User.objects.get(username=username), message=request.user.username + ' is now following you', url='/profile/' + request.user.username, img='follow.png') newNotification.save() alert = newNotification.user alert.numofnewnote += 1 alert.save() return Response( User.objects.get(username=username).profilePics.url) elif action == 'unfollow': fol = Following.objects.filter( member=request.user.username, follow=User.objects.get(username=username)) if fol: fol.delete() # Create a notification for unfollowing unfollowNote = Notification( user=User.objects.get(username=username), message=request.user.username + ' unfollowed you', url='/profile/' + request.user.username, img='follow.png') unfollowNote.save() # alert alert = unfollowNote.user alert.numofnewnote += 1 alert.save() return Response(True) else: return Response(False) else: return Response(False)
def post(self, request): serializer = WallSerializer(data=request.data) if serializer.is_valid(): serializer.save() # Ensuring notification is not created when the wall owner writes username = request.data['user'] # username of the user(wall owner) if not request.user.username == username: # Creating Notification for new wall post newNotification = Notification( user=User.objects.get(username=username), img='wall.png', url='/profile', message=request.data['writer'] + ' write on your wall') newNotification.save() # alert alert = newNotification.user alert.numofnewnote += 1 alert.save() else: for follower in Following.objects.filter( follow=User.objects.get(username=username)): newNotification = Notification( user=User.objects.get(username=follower.member), message=follower.follow.username + ' write their wall', url='/profile/' + follower.follow.username, img='wall.png') newNotification.save() # alert alert = User.objects.get(username=follower.member) alert.numofnewnote += 1 alert.save() writer = User.objects.get(pk=serializer.data['writer']) newData = serializer.data newData['username'] = writer.username if not writer.profilePics: newData['profilePics'] = '/static/index/media/default.png' else: newData['profilePics'] = writer.profilePics.url print('New wall data') print(newData) return Response(newData, status=status.HTTP_201_CREATED) return Response(False)
def post(self, request): serializer = WallReplySerializer(data=request.data) if serializer.is_valid(): serializer.save() newData = serializer.data # Creating Notification for owner of the write up wallOwner = Wall.objects.get(pk=newData['wall']).user if wallOwner != User.objects.get(pk=newData['writer']).username: ownerNote = Notification( user=Wall.objects.get(pk=newData['wall']).writer, message=request.user.username + ' reply on your write up on ' + wallOwner + ' wall', url='/profile/' + wallOwner, img='wall.png') ownerNote.save() # alert for the wall writer alert = ownerNote.user alert.numofnewnote += 1 alert.save() wallerNote = Notification( user=User.objects.get(username=wallOwner), message=request.user.username + ' reply on a write up on your wall', url='/profile', img='wall.png') wallerNote.save() # alert for the wall owner alert = wallerNote.user alert.numofnewnote += 1 alert.save() # getting the profilePics of the writer writer = User.objects.get(pk=newData['writer']) if not writer.profilePics: newData['profilePics'] = '/static/index/media/default.png' else: newData['profilePics'] = writer.profilePics.url newData['writer'] = writer.username newData['replybody'] = newData['body'] del newData['body'] print('Wall reply') print(newData) return Response(newData, status=status.HTTP_201_CREATED) return Response(False)
def seller_ok_views(request, order_id): if request.method == 'POST': user = User.objects.get(username=request.session['username']) order = Order.objects.get(id=order_id) if user != order.good.creator: messages.error(request, '没有权限') return render(request, '/', locals()) good =order.good good = Good.objects.get(id = good.id) receiver = order.creator file = good.file if file: file = File.objects.get(id =file.id) print(file.file_bedown) f = File( file_name=file.file_name, file_size=file.file_size, file_bedown = file.file_bedown, file_classify = file.file_classify, file = file.file, user = receiver ) f.save() order.buyer_ok = True order.status = 2 order.save() good = order.good good.sell_times += 1 good.save() buyer = order.creator buyer.trade_count += 1 buyer.save() seller = good.creator seller.trade_count += 1 seller.save() #消息处理 if file: send_ok = Notification(aim_user=buyer, arg0=20, arg1=order_id, arg4=buyer,content="文件已发放至您的网盘中") send_ok.save() n = Notification(aim_user=buyer, arg0=3, arg1=order_id, arg4=user) n.save() return redirect(reverse('paying', args=(order.id,)))
def get(self, request, action, post_id): if not Post.objects.filter(pk=post_id): # Checking if the post exist return False post = Post.objects.get(pk=post_id) if action == 'favour': print('Favour') user = User.objects.get(username=request.user.username) viewed = View.objects.filter(user=user, post=post) if not viewed: newview = View(user=user, post=post) newview.save() if Favour.objects.filter(favouredBy=user, post=post): return Response(False) else: favourid = Favour(favouredBy=user, post=post) favourid.save() # Creating Notification for post being favoured if Post.objects.get( pk=post_id).user.username != request.user.username: newNote = Notification( user=Post.objects.get(pk=post_id).user, message=user.username + ' favoured your post on ' + Post.objects.get(pk=post_id).postType, url='/post/' + str(post_id), img='like.png') newNote.save() alert = newNote.user alert.numofnewnote += 1 alert.save() return Response(True) elif action == 'unfavour': print('Unfavour') user = User.objects.get(username=request.user.username) if Favour.objects.filter(favouredBy=user, post=post): Favour.objects.get(favouredBy=user, post=post).delete() return Response(True) else: return Response(False) else: return Response(False)
def post(self, request): serializer = PostCommentSerializer(data=request.data) if serializer.is_valid(): serializer.save() post_id = serializer.data['targetpost'] user = User.objects.get(username=request.user.username) post = Post.objects.get(pk=post_id) post_date = post.date viewed = View.objects.filter(user=user, post=post) if not viewed: newview = View(user=user, post=post) newview.save() # Ensuring the post author doesn't get a note when he give a comment if request.user.username != Post.objects.get( pk=post_id).user.username: # Creating Notification for the author of the post commented on. newNote = Notification(user=Post.objects.get(pk=post_id).user, message=request.user.username + ' commented on your post named ' + Post.objects.get(pk=post_id).title, url='/post/' + str(post_id), img='comment.png') newNote.save() alert = newNote.user alert.numofnewnote += 1 alert.save() # list to ensure no repetition of note to one commentator. givento = [] for commentator in Postcomment.objects.filter( targetpost=Post.objects.get(pk=post_id)): # Ensuring the owner of post doesn't get a notification because they also commented if commentator.author.username != Post.objects.get( pk=post_id).user.username: # Ensuring the new commentator doesn't get a note if he has given a comment before. if request.user.username != commentator.author.username: if commentator.author.username not in givento: newNotification = Notification( user=commentator.author, message=request.user.username + ' gave a comment on ' + Post.objects.get(pk=post_id).title + ' you are following', url='/post/' + str(post_id), img='comment.png') newNotification.save() alert = newNotification.user alert.numofnewnote += 1 alert.save() givento.append(commentator.author.username) newData = serializer.data authorObj = User.objects.get(pk=newData['author']) newData['author'] = authorObj.username if authorObj.profilePics: newData['profilePic'] = authorObj.profilePics.url else: newData['profilePic'] = '/static/index/media/default.png' newData['comDate'] = datetime.datetime.now() newData['comBody'] = newData['body'] del newData['body'] return Response(newData) else: return Response(False)