Beispiel #1
0
def send_message_to_org(id):
    recipient_org = Organization.query.filter_by(id=id).first_or_404()
    form = MessageForm()
    if form.validate_on_submit():
        thread = Thread()
        db.session.add(thread)
        recipients = User.query
        for recipitent in recipients:
            recipitent.add_thread(thread)
        current_user.add_thread(thread)
        msg = Message(sender=current_user,
                      recipient_org=recipient_org,
                      thread=thread,
                      body=form.message.data)
        db.session.add(msg)

        recipient.add_notification('unread_message_count',
                                   recipient.new_messages_count())

        db.session.commit()
        flash('Your message has been sent.')
        return redirect(url_for('messages.messages'))
    return render_template('messages/send_message.html',
                           title='Send Message',
                           form=form,
                           recipient_org=recipient_org)
Beispiel #2
0
def create_welcome_message(user_id):

    user = User.query.get(user_id)

    message_html = render_template(
        'welcome_message.html',
        church_image=url_for('static',
                             filename='images/dutch_reformed_church.jpg',
                             _external=True),
        user=user)

    subject = 'Welcome to Maranatha Namibia'

    message = Message(created_by=user_id,
                      subject=subject,
                      content=message_html,
                      message_type=1,
                      is_urgent=False)

    db.session.add(message)
    db.session.commit()

    user_message = UserMessage(user_id=user_id,
                               message_id=message.id,
                               sent=False,
                               read=False)

    db.session.add(user_message)
    db.session.commit()

    return message.id
Beispiel #3
0
def upload_verses():
    # app = create_app()

    path = "E:\\Dropbox\\FlaskProjects\\maranatha\\app\\setup\\verses.txt"

    users = User.query.all()

    with codecs.open(path, 'r', 'utf-8') as verses:
        lines = verses.readlines()
        line_iter = iter(lines)
        while True:
            try:
                text = next(line_iter).strip()
                verse = next(line_iter).strip()
                next(line_iter).strip()
                user = users[randint(0, len(users) - 1)]
                delta = timedelta(days=randint(0, 60),
                                  hours=randint(0, 23),
                                  minutes=randint(0, 60))
                time = datetime.utcnow() - delta
                message = Message(subject=text,
                                  content=verse,
                                  created_by=user.id,
                                  created_at=time)
                db.session.add(message)
                print(text, verse)
            except StopIteration:
                break
        db.session.commit()
Beispiel #4
0
def team_members():
    enlister = User.query.filter_by(id=current_user.invited_by).first()
    partners = []
    if enlister is not None:
        partners = User.query.filter_by(
            invited_by=enlister.id,
            is_active=True).order_by('first_name').all()
        for user in partners:
            if user.id == current_user.id:
                partners.remove(user)
        partners.append(enlister)
    enlistees = User.query.filter_by(invited_by=current_user.id,
                                     is_active=True).all()
    form = MessageForm()
    if form.validate_on_submit():
        user_ids = form.user_list.data
        message = Message(subject=form.subject.data,
                          content=form.content.data,
                          message_type=1,
                          created_by=current_user.id,
                          is_urgent=False)
        db.session.add(message)
        db.session.commit()
        message_id = message.id
        for id in user_ids.split(","):
            user_message = UserMessage(message_id=message_id, user_id=int(id))
            db.session.add(user_message)
        db.session.commit()
        flash('Message was successfully submitted', 'success')
        return redirect(url_for('main.home'))
    return render_template('my_team.html',
                           enlister=enlister,
                           partners=partners,
                           enlistees=enlistees,
                           form=form)
Beispiel #5
0
def view_message(thread_id: str):
    current_user.add_notification('unread_message_count',
                                  current_user.new_messages_count())
    db.session.commit()
    form = MessageForm()

    if isint(thread_id):
        thread = current_user.threads.filter_by(id=thread_id).first_or_404()

    else:
        recipient = User.query.filter_by(username=thread_id).first_or_404()
        thread = recipient.threads.intersect(current_user.threads).first()
        if not thread:
            thread = Thread()
            db.session.add(thread)
            recipient.add_thread(thread)
            recipient.add_notification('unread_message_count',
                                       recipient.new_messages_count())
            current_user.add_thread(thread)
            db.session.commit()
            return redirect(
                url_for('messages.view_message', thread_id=recipient.username))

    recipients = User.query.filter(User.id != current_user.id).filter(
        User.threads.any(Thread.id == thread.id)).all()

    for message in thread.messages:
        if not message.get_read_time(current_user):
            message.set_read_time(current_user, datetime.utcnow())

    if form.validate_on_submit():
        thread.last_updated = datetime.utcnow()
        for recipient in recipients:
            msg = Message(sender=current_user,
                          thread=thread,
                          body=form.message.data)
            db.session.add(msg)
            msg.add_recipient(recipient)
            recipient.add_notification('unread_message_count',
                                       recipient.new_messages_count())
            db.session.commit()
        flash('Your message has been sent.')
        return redirect(url_for('messages.view_message', thread_id=thread.id))
    return render_template('messages/view_message.html',
                           thread=thread,
                           recipients=recipients,
                           form=form)
Beispiel #6
0
    def create_message(self, sending_user, receiving_user, text, status):
        '''
        Create a message instance
        '''

        with sa_session(self.scoped_session) as session:
            message = Message(sending_user=sending_user,
                              receiving_user=receiving_user,
                              text=text,
                              status=status)

            session.add(message)
Beispiel #7
0
def create_message(message_type):
    print(f'Received value {message_type}')
    form = MessageForm()
    if form.validate_on_submit():
        message = Message()
        form.populate_obj(message)
        message.created_by = current_user.id
        message.message_type = message_type
        message.created_at = datetime.datetime.utcnow()
        db.session.add(message)
        db.session.commit()
        message_id = message.id
        if form.is_urgent.data:
            send_broadcast_messages(message_id)
        return redirect(
            url_for('messages.view_messages',
                    message_type=message_type,
                    id=current_user.id))
    return render_template('create_message.html',
                           message_type=message_type,
                           form=form)
Beispiel #8
0
def send_message(recipient):
    user = User.query.filter_by(username=recipient).first_or_404()
    form = MessageForm()
    if form.validate_on_submit() and user != current_user:
        msg = Message(author=current_user,
                      recipient=user,
                      body=form.message.data)
        db.session.add(msg)
        user.add_notification('unread_message_count', user.new_messages())
        db.session.commit()
        flash(_('Your message has been sent'))
        return redirect(url_for('user.user', username=recipient))
    return render_template('send_message.html',
                           tittle=_('Send Message'),
                           form=form,
                           recipient=recipient)
Beispiel #9
0
 def add_message(self, contents):
     message = Message.create(user=self, body=contents)
     return message