# 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


@login_required(login_url='/session/login/')
def make_group(request):
    if request.method != "POST":
        return render(request, 'session/make_group.html')
    group_name = request.POST['groupname']
    try:
        Group.objects.get(name=group_name)
        return render(request, 'session/make_group.html',
                      {'error':
                       'The name is already being used by other group'})
    except Group.DoesNotExist:
        new_group = Group.objects.create(name=group_name)
    if request.POST['groupname'] != "":
        new_group.add_member(request.user.userprofile)
    return redirect('/session/group/message/'+new_group.name+'/manage')


# main view of session/group
@login_required(login_url='/session/login/')
def view_group_list(request):
    mygroups = request.user.userprofile.groups.all()
    return render(request, 'session/grouplist.html', {'groups': mygroups})


@login_required(login_url='/session/login/')
def group_message(request, groupname):
    try:
        group = Group.objects.get(name=groupname)

# 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



@login_required(login_url='/session/login/')
def make_group(request):
    if request.method != "POST":
        return render(request, 'session/make_group.html')
    group_name = request.POST['groupname']
    try:
        Group.objects.get(name=group_name)
        return render(request, 'session/make_group.html',
                      {'error':
                       'The name is already being used by other group'})
    except Group.DoesNotExist:
        new_group = Group.objects.create(name=group_name)
    if request.POST['groupname'] != "":
        new_group.add_member(request.user.userprofile)
    return redirect('/session/group/message/'+new_group.name+'/manage')


# main view of session/group
@login_required(login_url='/session/login/')
def view_group_list(request):
    mygroups = request.user.userprofile.groups.all()
    return render(request, 'session/grouplist.html', {'groups': mygroups})


@login_required(login_url='/session/login/')
def group_message(request, groupname):
    try:
        group = Group.objects.get(name=groupname)

    except Group.DoesNotExist: