# Path: social/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User)
#     profile_picture = models.ForeignKey(Picture, null=True, blank=True)
#     birthdate = models.DateField(null=True, blank=True)
#     bio = MarkdownField(null=True, blank=True)
#     gender = models.CharField(max_length=6, null=True, blank=True, choices=(("Male", "Male"), ("Female", "Female")))
#     spoken_languages = models.CharField(max_length=150, null=True, blank=True)
#     hometown = models.CharField(max_length=40, null=True, blank=True)
#     home_country = models.CharField(max_length=25, null=True, blank=True)
#     home_state = models.CharField(max_length=30, null=True, blank=True)
#     current_town = models.CharField(max_length=40, null=True, blank=True)
#     current_country = models.CharField(max_length=25, null=True, blank=True)
#     current_state = models.CharField(max_length=30, null=True, blank=True)
#     friends = models.ManyToManyField(User, blank=True, related_name='friends')
#     subscriptions = models.ManyToManyField(User, blank=True, related_name='subscriptions')
#     invites = models.ManyToManyField(Invite, blank=True, related_name='invites')
#     event_invites = models.ManyToManyField(EventInvite, blank=True, related_name='event_invites')
#     albums = models.ManyToManyField(Album, blank=True)
#     events = models.ManyToManyField(Event, blank=True)
#     relationship_status = models.ForeignKey(Relationship, null=True, blank=True)
#     schools = models.ManyToManyField(School, through=SchoolMembership, blank=True)
#     employers = models.ManyToManyField(Employer, through=EmployerMembership, blank=True)
#     messages = models.ManyToManyField(Message, blank=True)
#     comments = models.ManyToManyField(Comment, blank=True)
#     groups = models.ManyToManyField(Group, blank=True)
#     site_language = models.ForeignKey(Language, null=True, blank=True)
#     url = models.CharField(max_length=20, unique=True)
# 
#     def __unicode__(self):
#         return "%s %s" % (self.user.first_name, self.user.last_name)
# 
# class Message(models.Model):
#     class Meta:
#         get_latest_by = 'sent'
#         ordering = ['-sent']
# 
#     author = models.ForeignKey(User, related_name='mfrom')
#     recipient = models.ForeignKey(User, related_name='mto')
#     subject = models.CharField(max_length=50)
#     body = models.TextField()
#     read = models.BooleanField()
#     sent = models.DateTimeField()
# 
#     def __unicode__(self):
#         return subject


from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from .models import UserProfile, Message
import forms as myforms
import datetime
    m = get_object_or_404(Message, pk=msg_id)
    # Don't allow just anyone to read messages
    if m not in user.userprofile.messages.all():
        return HttpResponseRedirect('/')
    else:
        if not m.read:
            m.read = True
            m.save()
        return render_to_response('messages/view.html', {'msg': m},
                context_instance=RequestContext(request))

@login_required
def msg_compose(request, msg_id=0):
    user = request.user
    form = myforms.MessageForm()
    if request.method == 'POST':
        now = datetime.datetime.now()
        msg = Message(author=user, sent=now, read=False)
        form = myforms.MessageForm(request.POST, instance=msg)
        if form.is_valid():
            m = form.save()
            recipient = form.cleaned_data['recipient']
            recipient.userprofile.messages.add(m)
            recipient.userprofile.save()
            return HttpResponseRedirect('/inbox/')

    # replying
    if msg_id > 0:
        m = Message.objects.get(id=msg_id)
        subject = 'Re: '+m.subject

# Path: social/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User)
#     profile_picture = models.ForeignKey(Picture, null=True, blank=True)
#     birthdate = models.DateField(null=True, blank=True)
#     bio = MarkdownField(null=True, blank=True)
#     gender = models.CharField(max_length=6, null=True, blank=True, choices=(("Male", "Male"), ("Female", "Female")))
#     spoken_languages = models.CharField(max_length=150, null=True, blank=True)
#     hometown = models.CharField(max_length=40, null=True, blank=True)
#     home_country = models.CharField(max_length=25, null=True, blank=True)
#     home_state = models.CharField(max_length=30, null=True, blank=True)
#     current_town = models.CharField(max_length=40, null=True, blank=True)
#     current_country = models.CharField(max_length=25, null=True, blank=True)
#     current_state = models.CharField(max_length=30, null=True, blank=True)
#     friends = models.ManyToManyField(User, blank=True, related_name='friends')
#     subscriptions = models.ManyToManyField(User, blank=True, related_name='subscriptions')
#     invites = models.ManyToManyField(Invite, blank=True, related_name='invites')
#     event_invites = models.ManyToManyField(EventInvite, blank=True, related_name='event_invites')
#     albums = models.ManyToManyField(Album, blank=True)
#     events = models.ManyToManyField(Event, blank=True)
#     relationship_status = models.ForeignKey(Relationship, null=True, blank=True)
#     schools = models.ManyToManyField(School, through=SchoolMembership, blank=True)
#     employers = models.ManyToManyField(Employer, through=EmployerMembership, blank=True)
#     messages = models.ManyToManyField(Message, blank=True)
#     comments = models.ManyToManyField(Comment, blank=True)
#     groups = models.ManyToManyField(Group, blank=True)
#     site_language = models.ForeignKey(Language, null=True, blank=True)
#     url = models.CharField(max_length=20, unique=True)
# 
#     def __unicode__(self):
#         return "%s %s" % (self.user.first_name, self.user.last_name)
# 
# class Message(models.Model):
#     class Meta:
#         get_latest_by = 'sent'
#         ordering = ['-sent']
# 
#     author = models.ForeignKey(User, related_name='mfrom')
#     recipient = models.ForeignKey(User, related_name='mto')
#     subject = models.CharField(max_length=50)
#     body = models.TextField()
#     read = models.BooleanField()
#     sent = models.DateTimeField()
# 
#     def __unicode__(self):
#         return subject


# Path: social/messaging.py
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from .models import UserProfile, Message
import forms as myforms
import datetime

    m = get_object_or_404(Message, pk=msg_id)
    # Don't allow just anyone to read messages
    if m not in user.userprofile.messages.all():
        return HttpResponseRedirect('/')
    else:
        if not m.read:
            m.read = True
            m.save()
        return render_to_response('messages/view.html', {'msg': m},
                context_instance=RequestContext(request))

@login_required
def msg_compose(request, msg_id=0):
    user = request.user
    form = myforms.MessageForm()
    if request.method == 'POST':
        now = datetime.datetime.now()
        msg = Message(author=user, sent=now, read=False)
        form = myforms.MessageForm(request.POST, instance=msg)
        if form.is_valid():
            m = form.save()
            recipient = form.cleaned_data['recipient']
            recipient.userprofile.messages.add(m)
            recipient.userprofile.save()
            return HttpResponseRedirect('/inbox/')

    # replying
    if msg_id > 0:
        m = Message.objects.get(id=msg_id)
        subject = 'Re: '+m.subject

        msg = Message(recipient=m.author, subject=subject)