1

I'm trying to implement a draft function on my simple message application which using a custom UserModel

A draft function is when the user doesn't send the mail straight away but saves it.

I'm having trouble figuring out how can I implement a draft function using my model.

I am able to figure out how to send a mail.

@login_required
def Create(request):
    person = Person.objects.get(user=request.user)
    form = NewMessageForm()
    if request.POST.get('send', False):
        form = NewMessageForm(request.POST)
        if form.is_valid():
            recipient = form.cleaned_data['recipient']
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']
            thread = Thread.objects.create(subject=subject,user=request.user)
            recipient = User.objects.get(username=recipient)
        message =    Message.objects.create(user=request.user,recipient=recipient,body=message,thread=thread)
            return HttpResponseRedirect(reverse('world:message'))

    elif request.POST.get('save', False):
        #How can I save it as a draft?

    return render(request,'create.html',{'messages':messages,'form':form,'person':person})

models

class Thread(models.Model):
    subject = models.CharField(max_length=100, blank=True)
    user = models.ForeignKey(User)


class Message(models.Model):
    user = models.ForeignKey(User, related_name='sender')
    recipient = models.ForeignKey(User, related_name='recipient')
    created = models.DateTimeField(auto_now_add=True)
    body = models.CharField(max_length=1000)
    read = models.BooleanField(default=False)
    trash = models.BooleanField(default=False)
    sentmessage = models.BooleanField(default=False)
    thread = models.ForeignKey(Thread)


    def __unicode__(self):
        return self.body

forms

class NewMessageForm(forms.ModelForm):
    recipient = forms.CharField(required=True,max_length=1)
    message = forms.CharField(widget=forms.Textarea,required=True,max_length=1)
    checkbox = forms.BooleanField(required=False)
    def clean_recipient(self):
        recipient = self.cleaned_data['recipient']
            try:
                recipient = User.objects.get(username=recipient)
            except User.DoesNotExist:
                raise forms.ValidationError("This username does not exist")
            return recipient
class Meta:
    model = Thread 
    fields = ('subject',)  

template

<form method="POST" >
    {% csrf_token %}
    {{form.recipient}}
    {{form.subject}}
    {{form.message}}
    <input type="submit" value="send" name="_send" />
    <input type="submit" value="save" name="_save" />
</form>
JackRoster
  • 1,603
  • 5
  • 16
  • 23

1 Answers1

1

You could add a BooleanField "draft" in Message which determine if the current message is a draft or not and filter the mailbox by exclude every message with the state draft and where recipient = current_user.

This needs to modify a bit your logic by set draft to 0 when the message is sent, 1 when the message is save.

EDIT: I didn't see your messagesent attribute. You could use it for that comportement, I think it's appropriate :)

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • I see so I put recipient as myself instead? – JackRoster Jul 02 '13 at 11:57
  • No, you just have to filter the list of messages seen by the recipient by excluding message with draft = 1. Something like `Messages.objects.filter(recipient=current_user, draft=0)` – Maxime Lorant Jul 02 '13 at 11:58
  • If the recipient isn't specified, you could set it as `None` by making the recipient field optional : http://stackoverflow.com/questions/6619984/can-i-make-the-foreign-key-field-optional-in-django-model I think this is a better choice ;) – Maxime Lorant Jul 02 '13 at 12:05
  • 1. Make the field optional too (`required=False`) 2. Check if the field contains something, in that case check if the user specified exists, else pass. – Maxime Lorant Jul 02 '13 at 12:15
  • max , Do you mind if you take a look at this please http://stackoverflow.com/questions/17426279/django-simple-messenging-app-is-not-raising-form-error – JackRoster Jul 02 '13 at 13:02