# 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 Group(models.Model):
#     members = models.ManyToManyField(UserProfile, related_name="groups")
#     name = models.CharField(max_length=30)
# 
#     # second argument(userprofile) can be one object or list of objects
#     def add_member(self, userprofile):
#         self.members.add(userprofile)
#         self.save()
# 
#     def remove_member(self, userprofile):
#         self.members.remove(userprofile)
#         self.save()
# 
# class GroupMessage(models.Model):
#     content = models.TextField()
#     sender = models.ForeignKey('UserProfile',
#                                related_name='group_message_sent')
#     receivers = models.ForeignKey('Group',
#                                   related_name='group_message_received')
#     created_time = models.DateTimeField(auto_now_add=True)
# 
#     def __str__(self):
#         return "Group Message from %s to %s at %s" % \
#             (self.sender, self.receivers, self.created_time)


from django.shortcuts import render, redirect
from apps.session.models import UserProfile, Group, GroupMessage
from django.contrib.auth.decorators import login_required
    return render(request, 'session/group_message.html',
                  {'group': group,
                   'me': request.user.userprofile,
                   'messages': messages})


@login_required(login_url='/session/login/')
def manage(request, groupname):
    try:
        group = Group.objects.get(name=groupname)
    except Group.DoesNotExist:
        return render(request, 'session/group_manage.html',
                      {'group': None, 'error': 'The group does not exist'})
    if not (request.user.userprofile in group.members.all()):
        return render(request, 'session/group_manage.html',
                      {'group': None,
                       'error': 'You are not a member of this group'})
    members = group.members.all().order_by('nickname')
    if request.method == "GET":
        return render(request, 'session/group_manage.html',
                      {'group': group, 'members': members})

    # DELETE request(remove self)
    if request.method == "DELETE":
        group.remove_member(request.user.userprofile)
        return redirect('/session/group/')

    # POST request(add member)
    try:
        invitee = UserProfile.objects.get(nickname=request.POST['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 Group(models.Model):
#     members = models.ManyToManyField(UserProfile, related_name="groups")
#     name = models.CharField(max_length=30)
# 
#     # second argument(userprofile) can be one object or list of objects
#     def add_member(self, userprofile):
#         self.members.add(userprofile)
#         self.save()
# 
#     def remove_member(self, userprofile):
#         self.members.remove(userprofile)
#         self.save()
# 
# class GroupMessage(models.Model):
#     content = models.TextField()
#     sender = models.ForeignKey('UserProfile',
#                                related_name='group_message_sent')
#     receivers = models.ForeignKey('Group',
#                                   related_name='group_message_received')
#     created_time = models.DateTimeField(auto_now_add=True)
# 
#     def __str__(self):
#         return "Group Message from %s to %s at %s" % \
#             (self.sender, self.receivers, self.created_time)


# Path: apps/session/group/views.py
from django.shortcuts import render, redirect
from apps.session.models import UserProfile, Group, GroupMessage
from django.contrib.auth.decorators import login_required

    return render(request, 'session/group_message.html',
                  {'group': group,
                   'me': request.user.userprofile,
                   'messages': messages})


@login_required(login_url='/session/login/')
def manage(request, groupname):
    try:
        group = Group.objects.get(name=groupname)
    except Group.DoesNotExist:
        return render(request, 'session/group_manage.html',
                      {'group': None, 'error': 'The group does not exist'})
    if not (request.user.userprofile in group.members.all()):
        return render(request, 'session/group_manage.html',
                      {'group': None,
                       'error': 'You are not a member of this group'})
    members = group.members.all().order_by('nickname')
    if request.method == "GET":
        return render(request, 'session/group_manage.html',
                      {'group': group, 'members': members})

    # DELETE request(remove self)
    if request.method == "DELETE":
        group.remove_member(request.user.userprofile)
        return redirect('/session/group/')

    # POST request(add member)
    try:
        invitee = UserProfile.objects.get(nickname=request.POST['nickname'])

    except UserProfile.DoesNotExist: