def post(self, request): if request.user.is_authenticated: user = get_object(Account, request.user.id) team = user.team_set.all() channel = Channel.objects.get(id=request.POST.get('channel_id', '')) thread = Thread() thread.name = request.POST.get('name', '') thread.save() thread.channel.add(channel) thread.save() message = Message() message.sender = user message.thread = thread message.text = request.POST.get('text', '') message.save() afres = {} afres['tread_id'] = thread.id afres['tread_name'] = thread.name afres['channel_id'] = channel.id afres['partner'] = message.sender.fio() afres['text'] = message.text return JsonResponse(afres)
def handle(self, *args, **options): faker = Faker("en_US") self.stdout.write("Start filling data") users = [] for index in range(options["len"]): self.stdout.write(f"Process {index} line of User") user, _ = User.objects.get_or_create( username=faker.profile()["username"]) user.first_name = faker.first_name() user.last_name = faker.last_name() user.email = faker.email() user.set_password("1234") user.save() users.append(user) for index in range(options["len"]): random_range = randrange(1000) self.stdout.write(f"Process {index} line of Chat -> " f"Message {random_range} ->? File") chat = Chat() chat.title = faker.sentence()[0:40] chat.creator = choice(users) chat.save() for _ in range(random_range): message = Message() message.sender = choice(users) message.chat = chat message.message = faker.text() message.save() if choice([True, False, False, False]): image_file = io.BytesIO() image = Image.new( "RGBA", size=(100, 100), color=( randrange(255), randrange(255), randrange(255), ), ) image.save(image_file, "png") image_file.name = "test.png" image_file.seek(0) file = File() file.document = files.File(image_file) file.message = message file.save() self.stdout.write("End filling data")
def create(self, request): channel_pk = request.POST['channel'] message = request.POST['message'] channel_object = Channel.objects.get(pk=channel_pk) newMessage = Message( user=request.user, channel=channel_object, message=message, ) newMessage.save() return HttpResponse(status=201)
def respond(request): saved = False people = 0 sessions = 0 message = '' if request.method == 'POST': form = BookForm(request.POST) if form.is_valid(): user_sender = User.objects.get(pk=request.POST['id_sender']) user_receiver = User.objects.get(pk=request.POST['id_receiver']) sent = True people = form.cleaned_data['people'] sessions = form.cleaned_data['sessions'] message = form.cleaned_data['message'] bm = Message() bm.id_sender = user_sender bm.id_receiver = user_receiver bm.message = message bm.people = people bm.sessions = sessions bm.save() mail_data = {} mail_data['subject'] = ' %s %s is Interested in you.' % (user_sender.first_name, user_sender.last_name) mail_data['message'] = ''' %s %s, <br> %s %s is Interested in talking to you. To answer check your inbox.<br> <h3>People: %s</h3> <br> <h3>Sessions: %s</h3> <br> <h3>Details: </h3><br> %s ''' % (user_receiver.first_name, user_receiver.last_name, user_sender.first_name, user_sender.last_name, people, sessions, message) mail_data['from'] = '*****@*****.**' mail_data['sender'] = user_sender.email mail_data['receiver'] = user_receiver.email sent = utils.send_mail(mail_data) saved = True return HttpResponseRedirect('/search/') else: return HttpResponseRedirect('/')
def receive(self, text_data): data = json.loads(text_data) chat_obj = Chat.objects.get(pk=data['chat']) sender = chat_obj.group.users.all().filter(username=self.user_name) if chat_obj and sender: sender = sender[0] datetimeObj = datetime.now() created_time = datetimeObj.strftime("%b %d %Y %I:%M%p") # set last chat used by user. sender.selected_chat = chat_obj sender.save() # Save message to database message_obj = Message() # sender is in list format, get first sender message_obj.sender = sender message_obj.chat = chat_obj message_obj.text = data['message'] if 'image' in data: # Generate a unique file name chars = string.ascii_letters + string.digits + '!@#$%^&*()' random.seed = (os.urandom(1024)) filename = ''.join(random.choice(chars) for i in range(23)) # Save to message object message_obj.image = base64_file(data=data['image'], name=filename) message_obj.save() # Emit message to online users for user in chat_obj.group.users.all(): async_to_sync(self.send_user_message)(user.username, message_obj, created_time)
def send_message(thread_id, sender_id, message_text, sender_name=None): message = Message() message.text = message_text message.thread_id = thread_id message.sender_id = sender_id message.save()