def messages(section_id, id): section = SectionRepository.find_by_id(section_id) if not section: abort(404) thread = MessageRepository.find_by_id(id) if not thread: abort(404) if thread.section_id != section.id: abort(404) messages = MessageRepository.find_messages_of_thread(id) form = NewMessageForm() if form.validate_on_submit(): message = Message() message.course_id = section.course_id message.section_id = section.id message.user_id = current_user.id message.section_only = thread.section_only message.title = thread.title message.thread_id = thread.id message.message = form.message.data message = MessageRepository.create(message) return redirect( url_for('section.messages', section_id=section_id, id=id)) return render_template("section/messages.html", section=section, messages=messages, form=form, current_user=current_user, thread=thread)
def add_message(req): content = req.POST.get('text', '') if content == '': return JsonResponse( { 'errorCode': 'invalid_request', 'errorMessage': 'Invalid request, no text parameter' }, status=400) thread, created = Thread.objects.get_or_create(profile=req.user.profile) # rate limit if (thread.is_pending): last_staff_msg = Message.objects.filter(is_staff=True, thread=thread.id) ratelimit_hour = timezone.now() - timedelta(hours=6) time_check = ratelimit_hour if (last_staff_msg and last_staff_msg[0].timestamp > ratelimit_hour): # chose last hour, or the last time a staff replied time_check = last_staff_msg[0].timestamp count = Message.objects.filter(timestamp__gt=time_check).count() if (count >= 5): return JsonResponse( { 'errorCode': 'ratelimit', 'errorMessage': 'Please wait for a response before trying to send further requests.' }, status=400) thread.is_pending = True thread.save() message = Message(thread=thread, content=content) message.save() template_email('*****@*****.**', settings.HELP_INCHARGE, '[Mag:help] : ' + req.user.email, 'admin_help_request', { 'user': req.user.profile, 'message': message }) return HttpResponse(status=200)
def post(self, id): message = self.db2.query(Message).get(id) if not message: return self.write('Have not found message %s' % id) form = ReplyMessageForm(self.request.arguments) if form.validate(): m = Message(sender=message.receiver, receiver=message.sender, subject=form.subject.data, content=form.content.data) self.db2.add(m) self.db2.commit() m.receiver.notify() self.db2.commit() url = self.reverse_url('message:outbox') return self.redirect(url) self.render('message/reply_message.html', tilte=_('Reply Message'), message=message, form=form)
def post(self): form = NewMessageForm(self.request.arguments) if form.validate(): # convert username to user id you = self.db2.query(User).filter_by( username=form.sendto.data).first() if you: m = Message(sender=self.current_user, receiver=you, subject=form.subject.data, content=form.content.data) self.db2.add(m) self.db2.commit() m.receiver.notify() self.db2.commit() url = self.reverse_url('message:outbox') return self.redirect(url) else: form.sendto.errors.append(_('No such user !')) # end if self.render('message/new_message.html', title=_('New Message'), form=form)
def post(self): receiver = None form = MessageForm(self) if form.validate(): receiver = self.db.query(User).filter_by( username=form.to.data).first() if receiver: T = MessageText(subject=form.subject.data, body=form.text.data) self.db.add(T) self.db.commit() M = Message(sender_id=self.current_user.id, receiver_id=receiver.id, text_id=T.id) self.db.add(M) # send a message to myself if receiver.id != self.current_user.id: M = Message(sender_id=self.current_user.id, receiver_id=receiver.id, text_id=T.id) M.isinbox = False self.db.add(M) receiver.notify() self.db.commit() url = self.reverse_url('message:inbox') return self.redirect(url) else: form.to.errors.append( self.trans(_('Can not find user %s')) % form.to.data) # end if d = { 'title': self.trans(_('Send message')), 'form': form, 'RECEIVER': receiver } self.render('message/send_message.html', **d)
def add_message(req): content = req.POST.get('text', '') if content == '': return JsonResponse({ 'errorCode': 'invalid_request', 'errorMessage': 'Invalid request, no text parameter' }, status=400) thread, created = Thread.objects.get_or_create(profile=req.user.profile) # rate limit if (thread.is_pending): last_staff_msg = Message.objects.filter(is_staff=True, thread=thread.id) ratelimit_hour = timezone.now() - timedelta(hours=6) time_check = ratelimit_hour if (last_staff_msg and last_staff_msg[0].timestamp > ratelimit_hour): # chose last hour, or the last time a staff replied time_check = last_staff_msg.timestamp count = Message.objects.filter(timestamp__gt=time_check).count() print(count) if (count >= 5): return JsonResponse({ 'errorCode': 'ratelimit', 'errorMessage': 'Please wait for a response before trying to send further requests.' }, status=400) thread.is_pending = True thread.save() message = Message(thread=thread, content=content) message.save() template_email('*****@*****.**', settings.HELP_INCHARGE, '[Mag:help] : ' + req.user.email, 'admin_help_request', {'user': req.user.profile, 'message': message}) return HttpResponse(status=200)
def post(self): receiver = None form = MessageForm(self) if form.validate(): receiver = self.db.query(User).filter_by(username=form.to.data).first() if receiver: T = MessageText( subject = form.subject.data, body = form.text.data ) self.db.add(T) self.db.commit() M = Message( sender_id = self.current_user.id, receiver_id = receiver.id, text_id = T.id ) self.db.add(M) # send a message to myself if receiver.id != self.current_user.id: M = Message( sender_id = self.current_user.id, receiver_id = receiver.id, text_id = T.id ) M.isinbox = False self.db.add(M) receiver.notify() self.db.commit() url = self.reverse_url('message:inbox') return self.redirect(url) else: form.to.errors.append( self.trans(_('Can not find user %s')) % form.to.data ) # end if d = { 'title': self.trans(_('Send message')), 'form': form, 'RECEIVER': receiver } self.render( 'message/send_message.html', **d )
def post(self): form = MessageForm(self) d = {'title': self.trans(_('Send a notice to all user')), 'form': form} if form.validate(): to = form.to.data T = MessageText(subject=form.subject.data, body=form.text.data) self.db.add(T) self.db.commit() current_user_id = self.current_user.id text_id = T.id M = Message(sender_id=current_user_id, receiver_id=None, text_id=text_id) M.isinbox = False self.db.add(M) # TODO: select method for U in self.db.query(User): M = Message(sender_id=current_user_id, receiver_id=U.id, text_id=text_id) self.db.add(M) U.notify() self.db.commit() url = self.reverse_url('message:notice') return self.redirect(url) # failed self.render('message/send_notice.html', **d)
def post(self): form = MessageForm(self) d = { 'title': self.trans(_('Send a notice to all user')), 'form': form } if form.validate(): to = form.to.data T = MessageText( subject = form.subject.data, body = form.text.data ) self.db.add(T) self.db.commit() current_user_id = self.current_user.id text_id = T.id M = Message( sender_id = current_user_id, receiver_id = None, text_id = text_id ) M.isinbox = False self.db.add(M) # TODO: select method for U in self.db.query(User): M = Message( sender_id = current_user_id, receiver_id = U.id, text_id = text_id ) self.db.add(M) U.notify() self.db.commit() url = self.reverse_url('message:notice') return self.redirect( url ) # failed self.render( 'message/send_notice.html', **d)
def post(self, ID): M = self.my_message( ID ) if not M: return self.write( self.trans(_('Can not find message %s')) % ID ) body = self.get_argument('text', '') if not body: return self.write( self.trans(_('No content write !')) ) subject = reply_regex.sub('', M.text.subject) subject = self.trans(_('Re: %s')) % subject T = MessageText( subject = subject, body = body ) self.db.add(T) self.db.commit() NewMsg = Message( sender_id = self.current_user.id, receiver_id = M.sender_id, text_id = T.id ) NewMsg.reply_id = M.id self.db.add(NewMsg) # do not send a message to myself if M.sender_id != self.current_user.id: NewMsg = Message( sender_id = self.current_user.id, receiver_id = M.sender_id, text_id = T.id ) NewMsg.reply_id = M.id NewMsg.isinbox = False self.db.add(NewMsg) M.sender.notify() self.db.commit() url = self.reverse_url('message:outbox') self.redirect(url)
def send_message(self, user): admin = self.db2.query(User).filter_by(username='******').first() if admin: welcome = self.db2.query(LuoYunConfig).filter_by(key='welcome_new_user').first() if welcome: wc = json.loads(welcome.value).get('text') m = Message( sender = admin, receiver = user, subject = _('Welcome to use LuoYun Cloud !'), content = wc ) self.db2.add(m) self.db2.commit() m.receiver.notify() self.db2.commit()
def threads(id): section = SectionRepository.find_by_id(id) if not section: abort(404) threads = MessageRepository.find_threads_of_section(section.id) form = NewThreadForSectionForm() if form.validate_on_submit(): message = Message() message.course_id = section.course_id message.section_id = section.id message.user_id = current_user.id message.section_only = form.section_only.data message.title = form.title.data message.message = form.message.data message = MessageRepository.create(message) return redirect(url_for('section.threads', id=id)) return render_template("section/threads.html", section=section, threads=threads, form=form, current_user=current_user)
def threads(id): course = CourseRepository.find_by_id(id) if not course: abort(404) threads = MessageRepository.find_threads_of_course(course.id) form = NewThreadForCourseForm() if form.validate_on_submit(): message = Message() message.course_id = course.id message.user_id = current_user.id message.title = form.title.data message.message = form.message.data message = MessageRepository.create(message) return redirect(url_for('course.threads', id=id)) return render_template("course/threads.html", course=course, threads=threads, form=form, current_user=current_user)
def send_message(self, user): admin = self.db2.query(User).filter_by(username='******').first() if admin: welcome = self.db2.query(LuoYunConfig).filter_by(key='welcome_new_user').first() if welcome: wc = json.loads(welcome.value).get('text') T = MessageText( subject = self.trans(_('Welcome to use LuoYun Cloud !')), body = wc ) self.db2.add(T) self.db2.commit() M = Message( sender_id = admin.id, receiver_id = user.id, text_id = T.id ) self.db2.add(M) user.notify() self.db2.commit()
def messages(course_id, id): course = CourseRepository.find_by_id(course_id) if not course: abort(404) thread = MessageRepository.find_by_id(id) if not thread: abort(404) if thread.course_id != course.id: abort(404) messages = MessageRepository.find_messages_of_thread(id) form = NewMessageForm() if form.validate_on_submit(): message = Message() message.course_id = course.id message.user_id = current_user.id message.title = thread.title message.thread_id = thread.id message.message = form.message.data message = MessageRepository.create(message) return redirect(url_for('course.messages', course_id=course_id, id=id)) return render_template("course/messages.html", course=course, messages=messages, form=form, current_user=current_user, thread=thread)
def post(self, ID): M = self.my_message(ID) if not M: return self.write(self.trans(_('Can not find message %s')) % ID) body = self.get_argument('text', '') if not body: return self.write(self.trans(_('No content write !'))) subject = reply_regex.sub('', M.text.subject) subject = self.trans(_('Re: %s')) % subject T = MessageText(subject=subject, body=body) self.db.add(T) self.db.commit() NewMsg = Message(sender_id=self.current_user.id, receiver_id=M.sender_id, text_id=T.id) NewMsg.reply_id = M.id self.db.add(NewMsg) # do not send a message to myself if M.sender_id != self.current_user.id: NewMsg = Message(sender_id=self.current_user.id, receiver_id=M.sender_id, text_id=T.id) NewMsg.reply_id = M.id NewMsg.isinbox = False self.db.add(NewMsg) M.sender.notify() self.db.commit() url = self.reverse_url('message:outbox') self.redirect(url)
#!venv/bin/python from flask import * from app import db from app.message.models import Message message = Message() message.content = "Welcome to Framgia Workshop !" db.session.add(message) db.session.commit()