def RegisterMessage(request): if request.method == 'POST': email = request.POST['email'] exist = User.objects.filter(username=email) name = request.POST['name'] last_name = request.POST['last_name'] message = request.POST['message'] try: user = User.objects.create_user(username=email, email=email, first_name=name, last_name=last_name, password=email) myuser = MyUser(user=user, active=0) myuser.save() except IntegrityError: user = authenticate(request, username=email, password=email) message = Message(user=myuser) message.message = message message.save() return render(request, 'registerMessage.html', {'state': 1}) else: return render(request, 'registerMessage.html', {'state': 0})
def fpo_view(request, slug): fpo = Fpo_Registeration.objects.get(pk=slug) by = fpo.fpo_username user = User.objects.get(username=by) by1 = user.id products = Product.objects.filter(product_by=by1) print(user.email) context = { 'fpo': fpo, 'products': products, 'total_items_in_cart': repeat(request) } if request.method == 'POST': email = request.POST.get('email') name = request.POST.get('name') sub = request.POST.get('subject') message1 = request.POST.get('message') number = request.POST.get('number') subject = sub msg = Message(from1=request.user.username, to=fpo, email=email, subject=sub, message=message1, phone=number) msg.save() return render(request, 'store/fpo_detail_view.html', context)
def send_message(request, recipient): title = 'Send message' if timezone.now() >= request.user.date_joined + timezone.timedelta( minutes=10): permission = Permission.objects.get(name="Send Private Message") request.user.user_permissions.add(permission) if not request.user.has_perm('users.send_private_message'): wait_sec = (timezone.timedelta(minutes=10) - (timezone.now() - request.user.date_joined)).seconds messages.add_message( request, messages.WARNING, 'New users must wait 24 hours before they can send private messages.' '(So that you can test this feature you can send a private ' f'message in {wait_sec//60:0>2} min and {wait_sec%60:0>2} sec)') return redirect('index') user = get_object_or_404(User, username=recipient) if request.method == "POST": form = MessageForm(request.POST) if form.is_valid(): msg = Message(sender=request.user, recipient=user, body=form.cleaned_data['message']) msg.save() messages.add_message(request, messages.SUCCESS, 'Your message has been sent.') return redirect('index') else: form = MessageForm() return render(request, 'users/send_message.html', { 'title': title, 'form': form, 'recipient': recipient })
def viewpage(request, slug): products = Product.objects.get(id=slug) user = User.objects.get(username=products.product_by) # print(products.product_by) fpo = Fpo_Registeration.objects.get(fpo_username=user) by = fpo.fpo_username similars = Product.objects.filter( product_category=products.product_category) similars2 = Product.objects.filter(product_by=products.product_by) context = { 'product': products, 'total_items_in_cart': repeat(request), 'similars': similars, 'similars2': similars2, 'fpo': fpo } if request.method == 'POST': email = request.POST.get('email') name = request.POST.get('name') sub = request.POST.get('subject') message1 = request.POST.get('message') number = request.POST.get('number') msg = Message(from1=request.user.username, to=fpo, email=email, subject=sub, message=message1, phone=number) msg.save() return render(request, 'store/viewpage.html', context)
def request_to_join(request, pid): project = Project.objects.get(pk = pid) msg = request.user.username + " would like to join your team for the project: " + project.title newMsg = Message(fromUser = request.user, toUser = project.createdBy, message = msg) newMsg.save() newRq = Request(requestedProject = project, requestedBy = request.user) newRq.save() url = "/projects/details/" + pid + "/" return HttpResponseRedirect(url)
def test_create_new_message(self): """Test the create_new_message method for creating a new message in a Conversation""" conversation = self.conversation sender = self.user_two Message.create_new_message(conversation_id=conversation.id, text="Received your message", sender_id=sender.id) # tests update_read_by self.assertEqual(sender.conversations_unread.count(), 0) self.assertEqual(self.user.conversations_unread.count(), 1)
def save_callback(sender, **kwargs): """ Django includes a "signal dispatcher" which helps allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place. This is a receiver function as a signal handler for post_save signal. Once triggered, it will notify all users if a new kindergarten is in the system, or notify all following users of a kindergarten if it has some updates. Decorator @receiver: signal: A signal or a list of signals to connect a function to. sender: A specific signal sender post_save signal get sent many times, but we are only interested in receiving update of kindergarten models. Args: sender: an HttpRequest object. kwargs: post_save signal will send following keyword arguments: instance: The actual instance being saved. created: A boolean; True if a new record was created. raw: A boolean; True if the model is saved exactly as presented (i.e. when loading a fixture). using: The database alias being used. update_fields: The set of fields to update as passed to Model.save(), or None if update_fields wasn't passed to save(). Returns: None """ print(kwargs) kindergarten = kwargs['instance'] msg = Message() msg.kindergarten = kindergarten if kwargs['created'] == True: msg.content = "A new kindergarten, " + kindergarten.name + ", is available now!" #msg.from_id = 1 msg.save() userlist = User.objects.all() print(userlist) notifyUser(userlist, msg) else: msg.content = "There is an update in the " + kindergarten.name + "!" #msg.from_id= 1 msg.save() userlist = getschoolfollower(kindergarten) notifyUser(userlist, msg)
def create(self, validated_data): user = self.context["request"].user user_id = user.id txt = validated_data.get("text") # Check if the user is a participant of the conversation # happens in the view via custom PermissionClass conversation_id = validated_data.get("conversation_id") obj = Message.create_new_message(conversation_id=conversation_id, text=txt, sender_id=user_id) return obj
def new_view(request): # Authentication check. authentication_result = views.authentication_check(request) if authentication_result is not None: return authentication_result # Get the template data from the session template_data = views.parse_session(request, {'form_button': "Send Message"}) if request.method == 'POST': form = MessageForm(request.POST) if form.is_valid(): message = Message( target=form.cleaned_data['target'], sender=request.user.account, header=form.cleaned_data['header'], body=form.cleaned_data['body'], ) message.save() logger.log(Action.ACTION_MESSAGE, 'Message sent', request.user) form = MessageForm() # Clean the form when the page is redisplayed template_data['alert_success'] = "Successfully sent your message!" else: form = MessageForm() template_data['form'] = form return render(request, 'page/message/new.html', template_data)
def post(self, request, order_id): order = Ordering.objects.get(id=order_id) order.is_pay = True order.status = "1" order.save() message = Message() message.send_to = order.bid message.message = "您有新的订单了,请注意查收" message.save() return render(request, "pay_success.html")
def post(self, request, order_id): order = Ordering.objects.get(id=order_id) status = request.POST.get("状态") message = Message() message.send_to = order.user_id if status == "1": order.status = "2" message.message = "商家已收到您的订单,正在为您准备菜品" elif status == "2": order.status = "3" message.message = "您的外卖正在配送中,请注意外卖骑手的通知" else: order.status = "4" message.message = "您的订单已完成,谢谢您的下次光临!" order.save() message.save() order_detail = OrderDetail.objects.filter(orderid_id=order_id) return render(request, "business_order_detail.html", { "order_detail": order_detail, "order": order })
def SendMessage(request): auth_user = CheckAuth(request) if auth_user == False: return HttpResponse(status=403) user = User.objects.get(init=CheckAuth(request)[0]['id']) companion = request.POST.get('companion', False) type_msg = request.POST.get('type', False) date = request.POST.get('date', False) triger_msg = request.POST.get('triger_msg', '') if triger_msg == '1': crypt_msg = True else: crypt_msg = False if companion and type_msg and date: if companion == "NaN": return HttpResponse(status=400) else: pass ''' def last_msg(insert,user,companion,type_msg): last_msg = LastMessage.objects.filter(companion_1=int(user.id),companion_2=int(companion)) | LastMessage.objects.filter(companion_1=int(companion),companion_2=int(user.id)) if not last_msg: insert = LastMessage(companion_1=int(user.id),companion_2=int(companion),text=request.POST.get('body',False),type_msg=type_msg,id_msg=insert.id) insert.save() else: last_msg.update(text=request.POST.get('body',False),type_msg=type_msg,id_msg=insert.id) ''' im = get_thumbnail(user.image, "50x50", crop="center") ################### # IF TEXT MESSAGE ################## if type_msg == "text": escape_string = html.escape(request.POST.get( 'body', False)).replace("<br>", "<br>") #body = crypto.Encrypt(escape_string) body = escape_string ###### IF GROUP ##### if request.GET.get('group', False) == '1': group = Group.objects.get(init=companion) users = Membership.objects.get(group=group.init) users_mass = [] for i in users.users.all(): if i.init != auth_user[0]['id']: users_mass.append(i.init) insert = Message( group=group.init, user=user.init, text=body, type_msg='1', date=date, img=im.url, delivered=True, crypting=crypt_msg, ) companion = users_mass group = group.init insert.save() for a in users_mass: usr = User.objects.get(init=a) insert.reading_group.add(usr) else: insert = Message( user=user.init, companion=companion, text=body, type_msg='1', date=date, img=im.url, delivered=True, crypting=crypt_msg, ) group = '' insert.save() #last_msg(insert,user,companion,type_msg) json_resp = { "group": str(group), "user": str(user.init), "companion": str(companion), "body": escape_string, "date": date, "type": type_msg, "id_msg": str(insert.id), "img": im.url, "crypting": crypt_msg, } return HttpResponse(json.dumps(json_resp)) ################### # IF FILE MESSAGE ################## if type_msg == "file": string = request.POST.get('body', False) #body = crypto.Encrypt(string) body = string if request.GET.get('group', False) == '1': group = Group.objects.get(init=companion) users = Membership.objects.get(group=group.init) users_mass = [] for i in users.users.all(): if i.init != auth_user[0]['id']: users_mass.append(i.init) insert = Message( group=group.init, user=user.init, text=body, type_msg='2', date=date, img=im.url, delivered=True, crypting=crypt_msg, ) companion = users_mass group = group.init insert.save() for a in users_mass: usr = User.objects.get(init=a) insert.reading_group.add(usr) else: insert = Message( user=user.init, companion=companion, text=body, type_msg='2', date=date, img=im.url, delivered=True, crypting=crypt_msg, ) group = '' insert.save() #last_msg(insert,user,companion,type_msg) json_resp = { "group": str(group), "user": str(user.init), "companion": str(companion), "body": body, "date": date, "type": type_msg, "id_msg": str(insert.id), "img": im.url, "crypting": crypt_msg, } return HttpResponse(json.dumps(json_resp)) #return HttpResponse('{"group":"'+str(group)+'","user":'******',"companion":'+str(companion)+',"body":"'+string+'","date":"'+date+'","type":"'+type_msg+'","id_msg":"'+str(insert.id)+'","img":"'+im.url+'"}') else: return HttpResponse(status=404)