# 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 Comment(models.Model):
#     class Meta:
#         get_latest_by = 'sent'
#         ordering = ['-sent']
# 
#     author = models.ForeignKey(User)
#     post = models.CharField(max_length=500)
#     read = models.BooleanField()
#     sent = models.DateTimeField()
#     public = models.BooleanField(default=False)
#     subcomments = models.ManyToManyField('SubComment', blank=True)
# 
# class SubComment(Comment):
#     parent = models.ForeignKey(Comment, related_name='parent')
# 
# Path: social/groups.py
# class GroupUpdates:
# 	#name = ""
# 	updates = []
# 
# 	#def __str__(self):
# 	#	return {name: updates}


from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.http import JsonResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.core import serializers
from django.forms.models import model_to_dict
from .models import UserProfile, Comment, SubComment
from .groups import GroupUpdates
import forms as myforms
import util
import datetime, json
    #up.sort(key=lambda x: x.sent, reverse=True)
    #up = serializers.serialize('json', up)
    #ng = {'No Group': up}
    #ng = GroupUpdates()
    #ng.name = 'No Group'
    #ng.updates = up
    updates.extend(up)
    return JsonResponse(updates, safe=False)

@login_required
def post(request, url):
    user = request.user
    prof = get_object_or_404(UserProfile, url=url)
    other_user = prof.user

    if util.can_users_interract(user, other_user):
        if request.method == 'POST':
            comment = Comment.objects.create(
                    author=user,
                    read=False,
                    sent = datetime.datetime.now()
                    )
            form = myforms.CommentForm(request.POST, instance=comment)
            comment = form.save()
            other_user.userprofile.comments.add(comment)
    return HttpResponseRedirect('/dashboard/')

@login_required
def reply(request, comment_id):
    user = request.user

# 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 Comment(models.Model):
#     class Meta:
#         get_latest_by = 'sent'
#         ordering = ['-sent']
# 
#     author = models.ForeignKey(User)
#     post = models.CharField(max_length=500)
#     read = models.BooleanField()
#     sent = models.DateTimeField()
#     public = models.BooleanField(default=False)
#     subcomments = models.ManyToManyField('SubComment', blank=True)
# 
# class SubComment(Comment):
#     parent = models.ForeignKey(Comment, related_name='parent')
# 
# Path: social/groups.py
# class GroupUpdates:
# 	#name = ""
# 	updates = []
# 
# 	#def __str__(self):
# 	#	return {name: updates}


# Path: social/comments.py
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.http import JsonResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.core import serializers
from django.forms.models import model_to_dict
from .models import UserProfile, Comment, SubComment
from .groups import GroupUpdates
import forms as myforms
import util
import datetime, json

    #up.sort(key=lambda x: x.sent, reverse=True)
    #up = serializers.serialize('json', up)
    #ng = {'No Group': up}
    #ng = GroupUpdates()
    #ng.name = 'No Group'
    #ng.updates = up
    updates.extend(up)
    return JsonResponse(updates, safe=False)

@login_required
def post(request, url):
    user = request.user
    prof = get_object_or_404(UserProfile, url=url)
    other_user = prof.user

    if util.can_users_interract(user, other_user):
        if request.method == 'POST':
            comment = Comment.objects.create(
                    author=user,
                    read=False,
                    sent = datetime.datetime.now()
                    )
            form = myforms.CommentForm(request.POST, instance=comment)
            comment = form.save()
            other_user.userprofile.comments.add(comment)
    return HttpResponseRedirect('/dashboard/')

@login_required
def reply(request, comment_id):
    user = request.user

    prof = get_object_or_404(UserProfile, url=url)