# 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
        messages = messages.exclude(sender=block.sender)
    for message in messages:
        message.read()
    return render(request,
                  'session/check_message.html', {'messages': messages})


@login_required(login_url='/session/login/')
def go_thread(request):
    if request.method != "POST":
        messages = Message.objects.filter(Q(sender=request.user.userprofile) |
                                          Q(receiver=request.user.userprofile))
        messages = messages.order_by('-created_time')
        users = []
        for message in messages:
            if message.sender != request.user.userprofile:
                user = message.sender
            elif message.receiver != request.user.userprofile:
                user = message.receiver
            if not (user in users):
                users.append(user)
        return render(request, 'session/go_thread.html', {'users': users})
    return redirect('/session/message/thread/'+request.POST['nickname']+'/')


@login_required(login_url='/session/login/')
def check_thread(request, nickname):
    me = request.user.userprofile
    try:
        you = UserProfile.objects.get(nickname=nickname)

# 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

        messages = messages.exclude(sender=block.sender)
    for message in messages:
        message.read()
    return render(request,
                  'session/check_message.html', {'messages': messages})


@login_required(login_url='/session/login/')
def go_thread(request):
    if request.method != "POST":
        messages = Message.objects.filter(Q(sender=request.user.userprofile) |
                                          Q(receiver=request.user.userprofile))
        messages = messages.order_by('-created_time')
        users = []
        for message in messages:
            if message.sender != request.user.userprofile:
                user = message.sender
            elif message.receiver != request.user.userprofile:
                user = message.receiver
            if not (user in users):
                users.append(user)
        return render(request, 'session/go_thread.html', {'users': users})
    return redirect('/session/message/thread/'+request.POST['nickname']+'/')


@login_required(login_url='/session/login/')
def check_thread(request, nickname):
    me = request.user.userprofile
    try:
        you = UserProfile.objects.get(nickname=nickname)

    except UserProfile.DoesNotExist: