# -*- coding: gb2312 -*-
from django.template import loader,Context
from django.http import HttpResponse,Http404
from django.views.generic.list_detail import object_list
from models import *
from django.contrib.auth.models import User,UserManager
from django.db import models
from django.db.models import *
from django.contrib import admin,auth
from decorator import *
from django.contrib.auth.decorators import *
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
def register(request):
if request.method == 'GET':
return render_to_response("register.html")
else:
post = request.POST.copy()
usrname = post["username"]
passwrd = post["password"]
first = post["first_name"]
last = post["last_name"]
email = post["email"]
intro = post["selfIntro"]
user = User.objects.create_user(usrname,email,passwrd)
profile = UserProfile.objects.create(user)
user.is_staff = True;
profile.selfIntro = intro
profile.save()
user.firstname = first
user.lastname = last
user.save()
userAuth = auth.authenticate(username=usrname, password=passwrd)
if userAuth is not None:
auth.login(request, userAuth)
else:
raise Http404
hws = Homework.objects.all()
for hw in hws:
StudentHomework.objects.create(user,hw)
return HttpResponseRedirect('../shomepage/')
@login_required
def sHomepageView(request):
user = request.user
userpro = user.get_profile()
# hws = StudentHomework.objects.filter(user=user)
# group = profile.group
# return render_to_response('StudentHomepage.html',{'group':group, 'user':user,'hws':hws,'userp':profile})
graded_hws = StudentHomework.objects.filter(state=4,user=user)
ungrade_hws = StudentHomework.objects.filter(state__range=(1,3),user=user)
return render_to_response('StudentHomepage.html',{'graded_hws':graded_hws,'ungrade_hws':ungrade_hws,'userpro':userpro})
@login_required
def groupView(request):
###change group
if request.method == 'POST':
groupName = request.POST['name']
#except (KeyError, Choice.DoesNotExist):
#something for exception
group = Group.objects.filter(name=groupName)[0]
profile = request.user.get_profile()
profile.group = group
profile.save()
return HttpResponseRedirect('../shomepage/')
else:
user = request.user
name = user.username
group = user.get_profile().group
groups = Group.objects.all()
path = os.path.join(os.path.dirname(__file__),'lock.sys').replace('\\','/')
f = open(path, 'r')
isLock = False
lock = f.read(1)
f.close()
if lock == '1':
isLock = True
return render_to_response('group.html', {'isLock':isLock,'name':name, 'group':group, 'groups':groups})
@login_required
#@user_passes_test(user_can_createGroup,login_url="../Already_In_A_Group")
def createGroup(request):
if request.method == 'GET':
t = loader.get_template('createGroup.html')
c = Context()
return HttpResponse(t.render(c))
else:
post = request.POST.copy()
n = post["name"]
i = post["intro"]
user = request.user
group = Group.objects.create(name = n, introduction = i,leader = user)
p = user.get_profile()
p.group = group
p.save()
return HttpResponseRedirect('../group/')
@login_required
@user_passes_test(user_in_group,login_url="../DoNotHaveAGroup")
def quitGroup(request):
if request.method == 'GET':
t = loader.get_template('quitGroup.html')
c = Context()
return HttpResponse(t.render(c))
else:
user = request.user
pro = user.get_profile()
group = pro.group
group.quit(user)
pro.group = Group.objects.filter(name="DefaultGroup")[0]
pro.save()
return HttpResponseRedirect('../group/')
def update_tempgrade(u,userprofiles,hw):
grade = 0
for up in userprofiles:
if InternalGrade.objects.filter(user_grade=up.user,user_begrade=u.user,homework=hw):
ig = InternalGrade.objects.filter(user_grade=up.user,user_begrade=u.user,homework=hw)[0]
grade = grade + ig.grade
StudentHomework.objects.filter(homework=hw,user=u.user).update(tempgrade=grade)
return grade
@login_required
def gradeEachOtherView(request):
if request.method == 'GET':
if 'shid' in request.GET and request.GET['shid']:
shid = request.GET['shid']
request.session['shid'] = shid
else:
shid = request.session['shid']
shw = StudentHomework.objects.get(id=shid)
shw_unsub_shw = StudentHomework.objects.filter(homework=shw.homework,state__range=(1,3))
shw_sub_shw = StudentHomework.objects.filter(homework=shw.homework,state=4)
shw_sub_users =[]
shw_unsub_users = []
for s in shw_sub_shw:
shw_sub_users.append(s.user)
for s in shw_unsub_shw:
shw_unsub_users.append(s.user)
up = UserProfile.objects.filter(user=request.user)[0]
userprofiles_sub = UserProfile.objects.filter(group=up.group,user__in=shw_sub_users)
userprofiles_unsub = UserProfile.objects.filter(group=up.group,user__in=shw_unsub_users)
internalGrades = InternalGrade.objects.filter(user_grade=request.user, homework=shw.homework)
return render_to_response('gradeeachother.html',{'internalGrades':internalGrades, 'shw':shw,'userprofiles_sub':userprofiles_sub,'shw_subs':shw_sub_shw,'userprofiles_unsub':userprofiles_unsub})
if request.method == 'POST':
shid = request.session['shid']
shw = StudentHomework.objects.get(id=shid)
user_grade = request.user
up = UserProfile.objects.filter(user=request.user)[0]
if up.group.name == "DefaultGroup":
raise Http404
userprofiles = UserProfile.objects.filter(group=up.group)
for u in userprofiles:
if not InternalGrade.objects.filter(user_grade=user_grade,user_begrade=u.user,homework=shw.homework):
gr = request.POST[u.user.username]
if not gr:
gr = 0
InternalGrade.objects.create(user_grade,u.user,shw.homework,gr)
else:
gr = request.POST[u.user.username]
if not gr:
pass
else:
InternalGrade.objects.filter(user_grade=user_grade,user_begrade=u.user,homework=shw.homework).update(grade=gr)
update_tempgrade(u,userprofiles,shw.homework)
return HttpResponseRedirect("../../hwdetail/"+shid)
###for hw submit
import os
import sys
def arrange_screenshot(pathname,path,username,shid):
print "rar x "+pathname+" "+path
# os.system("rar x "+pathname+" "+path)
os.popen("rar x "+pathname+" "+path)
print "CutyCapt --url=http://127.0.0.1:8000/site_media/user/"+username+"/index.html --out="+path+"index.jpg"
os.popen("CutyCapt --url=http://127.0.0.1/site_media/user/"+username+"/index.html --out="+path+"index.jpg")
StudentHomework.objects.filter(id=shid).update(picture="/site_media/user/"+username+"/index.jpg")
StudentHomework.objects.filter(id=shid).update(online_url="/site_media/user/"+username+"/index.html")
import os
def handle_uploaded_file(f,shid,user,file_name):
userpath = 'static/user/'+user.username+"/"
path = os.path.join(os.path.dirname(__file_