def mess(request, view_user): if 'username' in request.session: username = request.session['username'] mem = Member.objects.get(pk=view_user) if view_user == username: greeting = "Your" else: greeting = view_user + "'s" if mem.profile: texta = mem.profile.text else: texta = "" if 'text' in request.POST: text = request.POST['text'] useThis = request.POST['pm'] check = '0' if check == useThis: publicMess = PublicMessage(text=text) publicMess.receives = view_user publicMess.createdby = username publicMess.mytime = "Date: " + time.strftime("%d/%m/%Y") + " Time: " + time.strftime("%H:%M:%S") publicMess.save() mem.save() mem.publicMessage.add(publicMess) else: privMess = PrivateMessage(text=text) privMess.receives = view_user privMess.createdby = username privMess.mytime = "Date: " + time.strftime("%d/%m/%Y") + " Time: " + time.strftime("%H:%M:%S") privMess.save() mem.save() mem.privateMessage.add(privMess) messages = PrivateMessage.objects.filter(receives=view_user, createdby=username) publicMess = PublicMessage.objects.filter(receives=view_user) return render(request, 'social/message.html', { 'appname': appname, 'username': username, 'view_user' : view_user, 'messages' : messages, 'publicMessage': publicMess, 'greeting': greeting, 'profile': texta, 'loggedin': True} ) else: checkMessage = "User is not logged in, no access to view this message page! Please log in." return render(request, 'social/login.html', { 'appname':appname, 'checkMessage' : checkMessage, 'loggedin' : False} )
def messages(request): if 'username' in request.session: username = request.session['username'] template = loader.get_template('social/messages.html') member = Member.objects.get(pk=username) context = RequestContext(request, { 'appname': appname, 'username': username, 'Alltext' : member.username, 'loggedin': True }) # checks for another user has been requested. Calls a different method called mess. if 'view' in request.GET: return mess(request, request.GET['view']) # a message is being posted. On the users own page. if 'text' in request.POST: text = request.POST['text'] useThis = request.POST['pm'] check = '0' # posting a public message if check == useThis: # creating a new message and filling in the fields publicMess = PublicMessage(text=text) publicMess.receives = username publicMess.createdby = username publicMess.mytime = "Date: " + time.strftime("%d/%m/%Y") + " Time: " + time.strftime("%H:%M:%S") publicMess.save() member.save() # adds on to the users public message member.publicMessage.add(publicMess) else: # creating a private message, and filling its fields privMess = PrivateMessage(text=text) privMess.receives = username privMess.createdby = username privMess.mytime = "Date: " + time.strftime("%d/%m/%Y") + " Time: " + time.strftime("%H:%M:%S") privMess.save() member.save() # adds on to the users private message member.privateMessage.add(privMess) else: text = "" # retrieve all private and public messages messages = member.privateMessage.all() publicMess = member.publicMessage.all() # decided to use this. To retrieve all messages that the user received. # Therefore the user can see all its message that has been sent to him. Both public and private messPublic = PublicMessage.objects.filter(receives=username) myMessages = PrivateMessage.objects.filter(receives=username) return render(request, 'social/messages.html', { 'appname': appname, 'username': username, 'messages' : myMessages, 'publicMessage' : messPublic, 'loggedin': True} ) else: checkMessage = "User is not logged in, no access to view this mesages page! Please log in." return render(request, 'social/login.html', { 'appname':appname, 'checkMessage' : checkMessage, 'loggedin' : False} )