# Path: django_fordrop/forms.py
# class UserProfileForm(forms.ModelForm):
#     class Meta:
#         model = UserProfile
#         exclude = ('user', 'uuid')
# 
# class RequestInviteForm(forms.Form):
#     email = forms.fields.EmailField(label="")
# 
# Path: django_fordrop/models.py
# class UserProfile(models.Model):
#     user = models.ForeignKey(User, unique=True, null=True, blank=True)
#     uuid = models.CharField(max_length=255, null=True, blank=True)
#     name = models.CharField(max_length=255, null=True, blank=True)
#     email = models.EmailField(null=True, blank=True)
#     avatar = ImageField(upload_to='avatars', null=True, blank=True)
#     location = models.CharField(max_length=255, null=True, blank=True)
#     web = models.URLField(null=True, blank=True)
#     bio = models.TextField(max_length=140, null=True, blank=True)
#     is_first_login = models.BooleanField(default=True)
#     time_created = models.DateTimeField(auto_now_add=True)
#     time_updated = models.DateTimeField(auto_now=True)
#     def activity(self):
#         return {
#             "objectType" : "person",
#             "id": self.uuid,
#             "displayName": self.name,
#             "location": self.location,
#             'web': self.web,
#             "bio": self.bio,
#         }
#     def __unicode__(self):
#         return '%s' % self.user
#     def get_absolute_url(self):
#         return '/profile/%s' % self.id
#     def save(self, *args, **kwargs):
#         super(UserProfile, self).save()
#         if not self.uuid:
#             self.uuid = uuid.uuid4().urn
#         if not self.email:
#             self.email = self.user.email
#         super(UserProfile, self).save()
#     class Admin:
#         pass
# 
# class UserSettings(models.Model):
#     user = models.ForeignKey(User)
#     notify_comment_on_file = models.BooleanField(default=False)
#     notify_comment_on_collection = models.BooleanField(default=False)
#     notify_same_file = models.BooleanField(default=False)
#     publish_avatar = models.BooleanField(default=False)
#     post_as_self = models.BooleanField(default=False)
#     time_created = models.DateTimeField(auto_now_add=True)
#     last_updated = models.DateTimeField(auto_now=True)
#     def __unicode__(self):
#         return '%s' % self.user
#     class Admin:
#         pass
# 
# class PubSubNode(models.Model):
#     node = models.CharField(max_length=255, unique=True)
#     name = models.CharField(max_length=255, unique=False)
#     time_created = models.DateTimeField(auto_now_add=True)
#     last_updated = models.DateTimeField(auto_now=True)
#     def __unicode__(self):
#         return '%s' % self.name
#     class Admin:
#         pass


from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
from django.core.mail import mail_admins
from django.db.models.query_utils import Q
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django_fordrop.forms import UserProfileForm, RequestInviteForm
from django_fordrop.models import UserProfile, UserSettings, PubSubNode
from forms import UploadFileForm, FileCommentForm, CollectionCommentForm, CollectionForm, FileTagForm, UserProfileForm, UserSettingsForm, CollectionTagForm
from models import handle_uploaded_file, File, Collection, xmpp, notify_by_mail
from django.conf import settings
import uuid
import urllib2
import json
import operator
@login_required
def profile(request, id=None):
    if not id:
        profile_user = request.user
    else:
        profile_user = User.objects.get(pk=id)
    if profile_user == request.user:
        tmpl = 'profile.html'
    else:
        tmpl = 'profile.html'
    files = File.objects.filter(user=profile_user).order_by('-time_updated')
    collections = Collection.objects.filter(followers__username=profile_user.username)
    return render_to_response(tmpl, {'profile_user': profile_user,
                                     'files': files,
                                     'collections': collections}, RequestContext(request))

@login_required
@csrf_exempt
def add_pubsub_node(request):
    if request.user.is_superuser:
        if request.method == 'POST':
            j = json.loads(request.POST.keys()[0])
            PubSubNode.objects.get_or_create(name=j.get('name'), node=j.get('node'))
        return HttpResponse(True)
    else:
        return PermissionDenied

@login_required
def edit_profile(request):
    profile, created = UserProfile.objects.get_or_create(user=request.user)

# Path: django_fordrop/forms.py
# class UserProfileForm(forms.ModelForm):
#     class Meta:
#         model = UserProfile
#         exclude = ('user', 'uuid')
# 
# class RequestInviteForm(forms.Form):
#     email = forms.fields.EmailField(label="")
# 
# Path: django_fordrop/models.py
# class UserProfile(models.Model):
#     user = models.ForeignKey(User, unique=True, null=True, blank=True)
#     uuid = models.CharField(max_length=255, null=True, blank=True)
#     name = models.CharField(max_length=255, null=True, blank=True)
#     email = models.EmailField(null=True, blank=True)
#     avatar = ImageField(upload_to='avatars', null=True, blank=True)
#     location = models.CharField(max_length=255, null=True, blank=True)
#     web = models.URLField(null=True, blank=True)
#     bio = models.TextField(max_length=140, null=True, blank=True)
#     is_first_login = models.BooleanField(default=True)
#     time_created = models.DateTimeField(auto_now_add=True)
#     time_updated = models.DateTimeField(auto_now=True)
#     def activity(self):
#         return {
#             "objectType" : "person",
#             "id": self.uuid,
#             "displayName": self.name,
#             "location": self.location,
#             'web': self.web,
#             "bio": self.bio,
#         }
#     def __unicode__(self):
#         return '%s' % self.user
#     def get_absolute_url(self):
#         return '/profile/%s' % self.id
#     def save(self, *args, **kwargs):
#         super(UserProfile, self).save()
#         if not self.uuid:
#             self.uuid = uuid.uuid4().urn
#         if not self.email:
#             self.email = self.user.email
#         super(UserProfile, self).save()
#     class Admin:
#         pass
# 
# class UserSettings(models.Model):
#     user = models.ForeignKey(User)
#     notify_comment_on_file = models.BooleanField(default=False)
#     notify_comment_on_collection = models.BooleanField(default=False)
#     notify_same_file = models.BooleanField(default=False)
#     publish_avatar = models.BooleanField(default=False)
#     post_as_self = models.BooleanField(default=False)
#     time_created = models.DateTimeField(auto_now_add=True)
#     last_updated = models.DateTimeField(auto_now=True)
#     def __unicode__(self):
#         return '%s' % self.user
#     class Admin:
#         pass
# 
# class PubSubNode(models.Model):
#     node = models.CharField(max_length=255, unique=True)
#     name = models.CharField(max_length=255, unique=False)
#     time_created = models.DateTimeField(auto_now_add=True)
#     last_updated = models.DateTimeField(auto_now=True)
#     def __unicode__(self):
#         return '%s' % self.name
#     class Admin:
#         pass


# Path: django_fordrop/views.py
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
from django.core.mail import mail_admins
from django.db.models.query_utils import Q
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django_fordrop.forms import UserProfileForm, RequestInviteForm
from django_fordrop.models import UserProfile, UserSettings, PubSubNode
from forms import UploadFileForm, FileCommentForm, CollectionCommentForm, CollectionForm, FileTagForm, UserProfileForm, UserSettingsForm, CollectionTagForm
from models import handle_uploaded_file, File, Collection, xmpp, notify_by_mail
from django.conf import settings
import uuid
import urllib2
import json
import operator

@login_required
def profile(request, id=None):
    if not id:
        profile_user = request.user
    else:
        profile_user = User.objects.get(pk=id)
    if profile_user == request.user:
        tmpl = 'profile.html'
    else:
        tmpl = 'profile.html'
    files = File.objects.filter(user=profile_user).order_by('-time_updated')
    collections = Collection.objects.filter(followers__username=profile_user.username)
    return render_to_response(tmpl, {'profile_user': profile_user,
                                     'files': files,
                                     'collections': collections}, RequestContext(request))

@login_required
@csrf_exempt
def add_pubsub_node(request):
    if request.user.is_superuser:
        if request.method == 'POST':
            j = json.loads(request.POST.keys()[0])
            PubSubNode.objects.get_or_create(name=j.get('name'), node=j.get('node'))
        return HttpResponse(True)
    else:
        return PermissionDenied

@login_required
def edit_profile(request):
    profile, created = UserProfile.objects.get_or_create(user=request.user)

    user_settings, created = UserSettings.objects.get_or_create(user=request.user)