# Path: niuauth/forms.py
# class ProfileForm(forms.Form):
#     avatar = forms.ImageField(required=False, label=_("Select a new picture"),
#                               help_text=_("The maximum image size you can upload is 2M"))
#     display_name = forms.CharField(required=False, label=_("Name"), max_length=64)
#     description = forms.CharField(widget=forms.Textarea(attrs={'rows':4,'placeholder': _('Tell a little about yourself')}),
#                                   required=False, label=_("Bio"), max_length=256)
#     website = forms.URLField(required=False, label=_('Website'))
#     company = forms.CharField(required=False, label=_("Company"), max_length=64)
#     email = forms.EmailField(required=False, label=_("E-mail address"))
#     location = forms.CharField(required=False, label=_("Location"), max_length=128)
#     github = forms.URLField(required=False, label=_('Github'))
#     gitlab = forms.URLField(required=False, label=_('Gitlab'))
# 
# Path: niuauth/models.py
# class Avatar(models.Model):
#     avatar_raw = models.ImageField("User upload avatar", upload_to='upload/%Y%m%d', blank=False, null=False, default="")
#     avatar_l = models.ImageField("large avatar", upload_to='avatar/%Y%m%d', blank=False, null=False, default="")
#     avatar_m = models.ImageField("medium avatar", upload_to='avatar/%Y%m%d', blank=False, null=False, default="")
#     avatar_s = models.ImageField("small avatar", upload_to='avatar/%Y%m%d', blank=False, null=False, default="")
#     deleted = models.BooleanField(default=False)
# 
# Path: niuauth/utils.py
# def user_reward(user, reward_type, topic_id=None, node_id=None):
#     amount = settings.REP_GET_SETTING[reward_type]
#     total = user.profile.reputation + amount
#     stat = ReputationStat(user=user, stat_type=reward_type, amount=amount, total=total,
#                           topic_id=topic_id, node_id=node_id)
#     stat.save()
#     user.profile.reputation = total
# 
# Path: forum/utils.py
# def create_thumbnail(src, new_name, ext):
#     upload = Image.open(BytesIO(src.read()))
#     fmt = src.content_type.split('/')[-1]
#     
#     large = _thumbnail(upload, (IMAGE_LARGE, IMAGE_LARGE), fmt)
#     filename_l = "%s_l.%s" % (new_name, ext)
#     large_file = SimpleUploadedFile(filename_l, large.read(), content_type=src.content_type)
#     
#     medium = _thumbnail(upload, (IMAGE_MEDIUM, IMAGE_MEDIUM), fmt)
#     filename_m = "%s_m.%s" % (new_name, ext)
#     medium_file = SimpleUploadedFile(filename_m, medium.read(), content_type=src.content_type)
#     
#     small = _thumbnail(upload, (IMAGE_SMALL, IMAGE_SMALL), fmt)
#     filename_s = "%s_s.%s" % (new_name, ext)
#     small_file = SimpleUploadedFile(filename_s, small.read(), content_type=src.content_type)
#     
#     return large_file, medium_file, small_file
# 
# def get_pagination(current_page, num_pages, count):
#     page_list = []
#     
#     show_pages = 2*count+1
#     if show_pages >= num_pages:
#         page_list.extend(range(1, num_pages+1))
#     elif current_page - count < 1:
#         page_list.extend(range(1, show_pages+1))
#     elif current_page + count > num_pages:
#         page_list.extend(range(num_pages+1-show_pages, num_pages+1))
#     else:
#         page_list.extend(range(current_page-count, current_page+count+1))
#     
#     return page_list


from django.views.generic.base import TemplateView, View
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.utils.crypto import get_random_string
from django.contrib.auth.models import User
from django.conf import settings
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import get_object_or_404
from niuauth.forms import ProfileForm
from niuauth.models import Avatar
from niuauth.utils import user_reward
from forum.utils import create_thumbnail, get_pagination
        large, medium, small = create_thumbnail(image, new_name, ext)
        a = Avatar(avatar_raw=image, avatar_l=large, avatar_m=medium, avatar_s=small)
        a.save()
        
        if profile.has_avatar():
            profile.avatar.deleted = True
            profile.avatar.save()
        
        profile.avatar = a

settings_profile = SettingsProfileView.as_view()

@method_decorator(login_required, name='dispatch')
class NotificationView(TemplateView):
    template_name = "niuauth/notification.html"
    
    def get_context_data(self, **kwargs):
        data = super(NotificationView, self).get_context_data(**kwargs)
        
        notifications = self.request.user.notifications.all()
        
        paginator = Paginator(notifications, 10)
        page = self.request.GET.get('page')
        try:
            noti_list = paginator.page(page)
        except PageNotAnInteger:
            noti_list = paginator.page(1)
        except EmptyPage:
            noti_list = paginator.page(paginator.num_pages)
        

# Path: niuauth/forms.py
# class ProfileForm(forms.Form):
#     avatar = forms.ImageField(required=False, label=_("Select a new picture"),
#                               help_text=_("The maximum image size you can upload is 2M"))
#     display_name = forms.CharField(required=False, label=_("Name"), max_length=64)
#     description = forms.CharField(widget=forms.Textarea(attrs={'rows':4,'placeholder': _('Tell a little about yourself')}),
#                                   required=False, label=_("Bio"), max_length=256)
#     website = forms.URLField(required=False, label=_('Website'))
#     company = forms.CharField(required=False, label=_("Company"), max_length=64)
#     email = forms.EmailField(required=False, label=_("E-mail address"))
#     location = forms.CharField(required=False, label=_("Location"), max_length=128)
#     github = forms.URLField(required=False, label=_('Github'))
#     gitlab = forms.URLField(required=False, label=_('Gitlab'))
# 
# Path: niuauth/models.py
# class Avatar(models.Model):
#     avatar_raw = models.ImageField("User upload avatar", upload_to='upload/%Y%m%d', blank=False, null=False, default="")
#     avatar_l = models.ImageField("large avatar", upload_to='avatar/%Y%m%d', blank=False, null=False, default="")
#     avatar_m = models.ImageField("medium avatar", upload_to='avatar/%Y%m%d', blank=False, null=False, default="")
#     avatar_s = models.ImageField("small avatar", upload_to='avatar/%Y%m%d', blank=False, null=False, default="")
#     deleted = models.BooleanField(default=False)
# 
# Path: niuauth/utils.py
# def user_reward(user, reward_type, topic_id=None, node_id=None):
#     amount = settings.REP_GET_SETTING[reward_type]
#     total = user.profile.reputation + amount
#     stat = ReputationStat(user=user, stat_type=reward_type, amount=amount, total=total,
#                           topic_id=topic_id, node_id=node_id)
#     stat.save()
#     user.profile.reputation = total
# 
# Path: forum/utils.py
# def create_thumbnail(src, new_name, ext):
#     upload = Image.open(BytesIO(src.read()))
#     fmt = src.content_type.split('/')[-1]
#     
#     large = _thumbnail(upload, (IMAGE_LARGE, IMAGE_LARGE), fmt)
#     filename_l = "%s_l.%s" % (new_name, ext)
#     large_file = SimpleUploadedFile(filename_l, large.read(), content_type=src.content_type)
#     
#     medium = _thumbnail(upload, (IMAGE_MEDIUM, IMAGE_MEDIUM), fmt)
#     filename_m = "%s_m.%s" % (new_name, ext)
#     medium_file = SimpleUploadedFile(filename_m, medium.read(), content_type=src.content_type)
#     
#     small = _thumbnail(upload, (IMAGE_SMALL, IMAGE_SMALL), fmt)
#     filename_s = "%s_s.%s" % (new_name, ext)
#     small_file = SimpleUploadedFile(filename_s, small.read(), content_type=src.content_type)
#     
#     return large_file, medium_file, small_file
# 
# def get_pagination(current_page, num_pages, count):
#     page_list = []
#     
#     show_pages = 2*count+1
#     if show_pages >= num_pages:
#         page_list.extend(range(1, num_pages+1))
#     elif current_page - count < 1:
#         page_list.extend(range(1, show_pages+1))
#     elif current_page + count > num_pages:
#         page_list.extend(range(num_pages+1-show_pages, num_pages+1))
#     else:
#         page_list.extend(range(current_page-count, current_page+count+1))
#     
#     return page_list


# Path: niuauth/views.py
from django.views.generic.base import TemplateView, View
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.utils.crypto import get_random_string
from django.contrib.auth.models import User
from django.conf import settings
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import get_object_or_404
from niuauth.forms import ProfileForm
from niuauth.models import Avatar
from niuauth.utils import user_reward
from forum.utils import create_thumbnail, get_pagination

        large, medium, small = create_thumbnail(image, new_name, ext)
        a = Avatar(avatar_raw=image, avatar_l=large, avatar_m=medium, avatar_s=small)
        a.save()
        
        if profile.has_avatar():
            profile.avatar.deleted = True
            profile.avatar.save()
        
        profile.avatar = a

settings_profile = SettingsProfileView.as_view()

@method_decorator(login_required, name='dispatch')
class NotificationView(TemplateView):
    template_name = "niuauth/notification.html"
    
    def get_context_data(self, **kwargs):
        data = super(NotificationView, self).get_context_data(**kwargs)
        
        notifications = self.request.user.notifications.all()
        
        paginator = Paginator(notifications, 10)
        page = self.request.GET.get('page')
        try:
            noti_list = paginator.page(page)
        except PageNotAnInteger:
            noti_list = paginator.page(1)
        except EmptyPage:
            noti_list = paginator.page(paginator.num_pages)
        

        page_list = get_pagination(noti_list.number, paginator.num_pages, 2)