Example #1
0
def front(request):

    # posts = Post.objects.all().order_by('-date_posted')

    context = {}

    if request.method == 'POST':

        # id = request.POST.get('id')

        # if id:

        #     message = Message.objects.get(id=id)
        #     form = PostForm(request.POST, request.FILES, instance=post)

        # else:

        form = MessageForm(request.POST)

        if is_admin(request.user):
            context['permission'] = 'is_admin'

        if form.is_valid():
            message = form.save()
            context['result'] = 'Your post has been saved'
        else:
            context['result'] = form.errors

    return render(request, 'index.html', context)
Example #2
0
def page_message_add(request):
    data = prepare_data(request)

    message_form = MessageForm(request.POST or None)
    if request.method == "POST":
        if message_form.is_valid():
            new_message = message_form.save()

            Log.objects.create(action=6, user=request.user, message=new_message, from_project=new_message.group.project,
                               from_group=new_message.group, from_account=new_message.group.project.from_account)

            messages.success(request, _("Created"))
            return redirect('message_add')
        else:
            messages.error(request, message_form.errors)

    content = {
        'form': message_form,
    }

    return render(request, "pages/page_message_add.html", {
        'title': _("Add new message"),
        'data': data,
        'content': content,
    })
Example #3
0
def create_message(request):
    messages = Message.objects.all()
    tags = Tag.objects.all()
    context = {'messages': messages, 'tags': tags, 'tagform': TagForm}
    form = MessageForm(request.POST, request.FILES, prefix="message")
    tags = TagForm(request.POST)
    if request.method == "POST":
        cleaned_tags = []
        if tags.is_valid():
            tag_list = []

            tag = tags.save(commit=False)

            words = tags.cleaned_data['word']
            tag_list = words.split(',')
            for word in tag_list:
                tag, created = Tag.objects.get_or_create(word=word)
                cleaned_tags.append(tag)

            context['tag'] = "Your tags have been saved"

        if form.is_valid():

            message = form.save()
            for tag in cleaned_tags:
                message.tag.add(tag)
            context['message'] = "Your message has been saved"
            return redirect('mbhome')
        else:
            context['message'] = form.errors

    return render(request, 'create-message.html', context)
Example #4
0
def editar(request, id):
    message = get_object_or_404(Message, pk=id)
    if request.method == "POST":
        form = MessageForm(request.POST, instance=message)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/")
    else:
        form = MessageForm(instance=message)

    data = {"form": form}
    return render_to_response("edit.html", data, context_instance=RequestContext(request))
Example #5
0
def agregar(request):
    if request.method == "POST":
        form = MessageForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/")
    else:
        form = MessageForm()

    data = {"form": form}

    return render_to_response("add.html", data, context_instance=RequestContext(request))
Example #6
0
def message_add(request, spirit):
    spirit = get_object_or_404(Spirit, slug=spirit)
    if request.method == 'POST':
        message_form = MessageForm(request.POST)
        message = message_form.save(commit=False)
        message.contributor = request.user.get_profile()
        if message.contributor.is_trusted:
            message.status = Message.Status.LIVE
        message.spirit = spirit
        message.save()
    message_form = MessageForm()
    title = 'Message Add'
    return locals()
Example #7
0
def send_message(recipient):
    user = User.query.filter_by(username=recipient).first_or_404()
    form = MessageForm()
    if form.validate_on_submit():
        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('main.user', username=recipient))
    return render_template('send_message.html', title=_('Send Message'),
            form=form, recipient=recipient)
Example #8
0
def message(request, send_to_user):
    form = MessageForm()
    print (request.get_full_path(), send_to_user)
    context = {'valid': True, 'form': form}

    if request.user.is_authenticated():
        user = user_to_fingr(request.user)
        context['authenticated'] = True
        target_user = FingrUser.objects.filter(pk=send_to_user)
        if len(target_user) > 0:
            target_user = target_user[0]
        else:
            target_user = None

        if target_user:
            if request.POST:
                form = MessageForm(request.POST)
                if form.is_valid():
                    print 'Message Sent'
                    context['message_sent'] = True
                    message_specific_friend(user, target_user, form.cleaned_data['message'])
                else:
                    return redirect('main.views.message')

            if target_user in user.friends_list:
                pass
            else:
                if target_user == user:
                    context['feedback'] = 'Wow you must be lonely to send a message to yourself :('
                    context['valid'] = False

                else:
                    context['feedback'] = "You are sending a message to someone who isn't your friend. They may not get this message"

            received = user.messages_list.filter(sentFrom=target_user,type=Message.MESSAGE).order_by('-time')
            sent = target_user.messages_list.filter(sentTo=target_user, sentFrom=user, type=Message.MESSAGE).order_by('-time')

            user.messages_list.filter(sentTo=user, type=Message.MESSAGE).update(read=True)
            conversation_history = sorted(chain(sent, received), key=lambda instance: instance.time, reverse=True)

            #conversation_history = received

            #get the previous messages
            context['conversation'] = conversation_history[0:5]
            context['talking_to'] = target_user
        else:
            context['valid'] = False
            context['feedback'] = 'No such user exists'
    else:
        return redirect('main.views.login')
    return render(request, 'message.html', context)
Example #9
0
def editar(request, id):
    message = Message.objects.get(pk=id)
    if request.method == 'POST':
        form = MessageForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/')
    else:
        form = MessageForm(instance=message)

    data = {
      'form': form,
      }

    return render_to_response("add.html", data, context_instance=RequestContext(request))
Example #10
0
def message():
    form = MessageForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        message = Message(title=title, body=body, user_id=current_user.id)
        db.session.add(message)
        db.session.commit()
        flash('已成功发送', 'info')
        return redirect(url_for('message'))
    page = request.args.get('page', 1, type=int)
    per_page = app.config['MESSAGE_PER_PAGE']
    pagination = Message.query.order_by(Message.timestamp.desc()).paginate(
        page, per_page=per_page)
    messages = pagination.items
    return render_template('message.html',
                           pagination=pagination,
                           messages=messages,
                           bs=bs,
                           form=form)
Example #11
0
 def test_message_form(self):
     form_data = {"message": "Test"}
     form = MessageForm(data=form_data)
     self.assertTrue(form.is_valid())