# Path: apps/session/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User,
#                                 primary_key=True, related_name='userprofile')
#     user = models.OneToOneField(User)
#     gender = models.CharField(max_length=1, choices=GENDER, default=ETC)
#     birthday = models.DateTimeField(blank=True, null=True)
# 
#     nickname = models.TextField(max_length=12)
#     points = models.IntegerField(default=0)
# 
#     def __str__(self):
#         return "User %s (%s)'s profile object" % (self.user, self.nickname)
# 
# class Message(models.Model):
#     content = models.TextField()
#     sender = models.ForeignKey('UserProfile', related_name='message_sent')
#     receiver = models.ForeignKey('UserProfile',
#                                  related_name='message_received')
#     created_time = models.DateTimeField(auto_now_add=True)
#     is_read = models.BooleanField()
# 
#     def __str__(self):
#         return "Message from %s to %s at %s" % \
#             (self.sender, self.receiver, self.created_time)
# 
#     def read(self):
#         self.is_read = True
#         self.save()
# 
# class Block(models.Model):
#     sender = models.ForeignKey('UserProfile',
#                                related_name='block_message_from')
#     receiver = models.ForeignKey('UserProfile',
#                                  related_name='block_message_to')
# 
#     def __str__(self):
#         return "%s blocked message from %s" % (self.receiver, self.sender)


from django.shortcuts import render, redirect
from apps.session.models import UserProfile, Message, Block
from django.contrib.auth.decorators import login_required
from django.db.models import Q
            message.read()
    return render(request, 'session/message_thread.html',
                  {'me': me,
                   'you': you,
                   'messages': messages})


@login_required(login_url='/session/login/')
def check_sent_message(request):
    messages = Message.objects.filter(sender=request.user.userprofile)
    return render(request,
                  'session/check_sent_message.html', {'messages': messages})


@login_required(login_url='/session/login')
def block(request):
    if request.method != "POST":
        return render(request, 'session/block.html')
    receiver = request.user.userprofile
    try:
        sender = UserProfile.objects.get(nickname=request.POST['nickname'])
    except UserProfile.DoesNotExist:
        return render(request, 'session/block.html',
                      {'error': "The user doesn't exist"})
    if sender == receiver:
        return render(request, 'session/block.html',
                      {'error': "You can't block yourself"})
    if len(Block.objects.filter(sender=sender, receiver=receiver)) != 0:
        return render(request, 'session/block.html',
                      {'error': "You already blocked the user"})

# Path: apps/session/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User,
#                                 primary_key=True, related_name='userprofile')
#     user = models.OneToOneField(User)
#     gender = models.CharField(max_length=1, choices=GENDER, default=ETC)
#     birthday = models.DateTimeField(blank=True, null=True)
# 
#     nickname = models.TextField(max_length=12)
#     points = models.IntegerField(default=0)
# 
#     def __str__(self):
#         return "User %s (%s)'s profile object" % (self.user, self.nickname)
# 
# class Message(models.Model):
#     content = models.TextField()
#     sender = models.ForeignKey('UserProfile', related_name='message_sent')
#     receiver = models.ForeignKey('UserProfile',
#                                  related_name='message_received')
#     created_time = models.DateTimeField(auto_now_add=True)
#     is_read = models.BooleanField()
# 
#     def __str__(self):
#         return "Message from %s to %s at %s" % \
#             (self.sender, self.receiver, self.created_time)
# 
#     def read(self):
#         self.is_read = True
#         self.save()
# 
# class Block(models.Model):
#     sender = models.ForeignKey('UserProfile',
#                                related_name='block_message_from')
#     receiver = models.ForeignKey('UserProfile',
#                                  related_name='block_message_to')
# 
#     def __str__(self):
#         return "%s blocked message from %s" % (self.receiver, self.sender)


# Path: apps/session/message/views.py
from django.shortcuts import render, redirect
from apps.session.models import UserProfile, Message, Block
from django.contrib.auth.decorators import login_required
from django.db.models import Q

            message.read()
    return render(request, 'session/message_thread.html',
                  {'me': me,
                   'you': you,
                   'messages': messages})


@login_required(login_url='/session/login/')
def check_sent_message(request):
    messages = Message.objects.filter(sender=request.user.userprofile)
    return render(request,
                  'session/check_sent_message.html', {'messages': messages})


@login_required(login_url='/session/login')
def block(request):
    if request.method != "POST":
        return render(request, 'session/block.html')
    receiver = request.user.userprofile
    try:
        sender = UserProfile.objects.get(nickname=request.POST['nickname'])
    except UserProfile.DoesNotExist:
        return render(request, 'session/block.html',
                      {'error': "The user doesn't exist"})
    if sender == receiver:
        return render(request, 'session/block.html',
                      {'error': "You can't block yourself"})
    if len(Block.objects.filter(sender=sender, receiver=receiver)) != 0:
        return render(request, 'session/block.html',
                      {'error': "You already blocked the user"})

    Block.objects.create(sender=sender, receiver=receiver)