#!/usr/bin/env python
#-*- coding: utf-8 -*-

from django.core.urlresolvers import reverse
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render_to_response,RequestContext
from django.contrib.auth.decorators import login_required
from swyzxwb_photovote.common.CommonPaginator import SelfPaginator
from UserManage.views.permission import PermissionVerify
from UserManage.xlsgroups import processxls
from UserManage.forms import GroupListForm,XLSFileForm,RemoveUsersForm
from UserManage.models import GroupList

@login_required
@PermissionVerify()
def AddGroup(request):
    if request.method == "POST":
        form = GroupListForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('listgroupurl'))
    else:
        form = GroupListForm()

    kwvars = {
        'form':form,
        'request':request,
    }

    return render_to_response('UserManage/group.add.html',kwvars,RequestContext(request))

@login_required
@PermissionVerify()
def ListGroup(request):
    mList = GroupList.objects.all()

    #分页功能
    lst = SelfPaginator(request,mList, 20)

    kwvars = {
        'lPage':lst,
        'request':request,
    }

    return render_to_response('UserManage/group.list.html',kwvars,RequestContext(request))

@login_required
@PermissionVerify()
def EditGroup(request,ID):
    iGroup = GroupList.objects.get(id=ID)

    if request.method == "POST":
        form = GroupListForm(request.POST,instance=iGroup)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('listgroupurl'))
    else:
        form = GroupListForm(instance=iGroup)

    kwvars = {
        'ID':ID,
        'form':form,
        'request':request,
    }

    return render_to_response('UserManage/group.edit.html',kwvars,RequestContext(request))

@login_required
@PermissionVerify()
def DeleteGroup(request,ID):
    GroupList.objects.filter(id = ID).delete()

    return HttpResponseRedirect(reverse('listgroupurl'))

@login_required
@PermissionVerify()
def XLSFileGroup(request):

    if request.method=='POST':
        form = XLSFileForm(request.POST,request.FILES)
        if form.is_valid():
            try:
                gdata=processxls(request.FILES['xls'])
            except Exception,e:
                kwvars = {'request':request,'xerror':e,}
                return render_to_response('UserManage/user.addxls.error.html',kwvars,RequestContext(request))
            try:
                querysetlist=[]
                for ud in gdata:
                    if ud[0]!=0:
                        querysetlist.append( GroupList(classid=str(ud[0]),name=ud[1]) )
                GroupList.objects.bulk_create(querysetlist)
                return HttpResponseRedirect(reverse('listgroupurl'))
            except Exception,e:
                kwvars = {'request':request,'xerror':e,}
                return render_to_response('UserManage/user.addxls.error.html',kwvars,RequestContext(request))
    else:
        form = XLSFileForm()

    kwvars = {
        'form':form,
        'request':request,
    }

    return render_to_response('UserManage/group.addxls.html',kwvars,RequestContext(request))


@login_required
@PermissionVerify()
def RemoveGroups(request):
    #get_user_model().objects.filter(id = ID).delete()
    if request.method=='POST':
        form = RemoveUsersForm(request.POST)
        if form.is_valid():
            try:
                for cid in form.cleaned_data["classid"]:
                    cid.delete()
                return HttpResponseRedirect(reverse('listgroupurl'))
            except Exception,e:
                kwvars = {'request':request,'xerror':e,}
                return render_to_response('UserManage/group.remove.error.html',kwvars,RequestContext(request))

    else:
        form = RemoveUsersForm()

    kwvars = {
        'form':form,
        'request':request,
    }

    return render_to_response('UserManage/group.removegroups.html',kwvars,RequestContext(request))
