Browse Source

Signed-off-by: zry <admin@z-touhou.org>

zry 8 years ago
parent
commit
5c27e2033e
5 changed files with 72 additions and 1 deletions
  1. 10 0
      genxls_temp.py
  2. BIN
      static/photoworks.xls
  3. 3 1
      vote/models.py
  4. 2 0
      vote/views.py
  5. 57 0
      vote/xlsadd.py

+ 10 - 0
genxls_temp.py

@@ -0,0 +1,10 @@
+# -*- coding: UTF-8 -*-
+import xlwt
+wb = xlwt.Workbook(encoding='utf-8')
+ws = wb.add_sheet("PhotoWorks")
+ws.write(0,0,u'作品ID')
+ws.write(0,1,u'参赛者用户名')
+ws.write(0,2,u'作品标题')
+ws.write(0,3,u'作品描述')
+ws.write(0,4,u'文件扩展名')
+wb.save("photoworks.xls")

BIN
static/photoworks.xls


+ 3 - 1
vote/models.py

@@ -12,7 +12,9 @@ class PhotoWorks(models.Model):
 	uuid = models.CharField(max_length=36,db_index=True)
 	title = models.CharField(max_length=255,db_index=True)
 	desc = models.TextField()
-	filename = models.CharField(max_length=255,db_index=True)
+	filename = models.CharField(max_length=64,db_index=True)
+	fileext = models.CharField(max_length=16)
+	importid = models.IntegerField()
 	score = models.IntegerField()
 
 	def __unicode__(self):

+ 2 - 0
vote/views.py

@@ -43,6 +43,8 @@ def ListContestants(request):
 
 	return render_to_response('vote/contestant.list.html',kwvars,RequestContext(request))
 
+
+
 def DeleteContestant(request,ID):
 	cobj = Contestants.objects.get(id=ID)
 	cobj.works_set.all().delete()

+ 57 - 0
vote/xlsadd.py

@@ -0,0 +1,57 @@
+import xlrd
+import datetime
+import uuid
+import os
+
+
+def process_cell(table,i,j):
+  _temp=table.cell(i,j)
+  if _temp.ctype==1:
+    return _temp.value
+  if _temp.ctype==2:
+    return str(int(_temp.value))
+  #raise "Error xls!"
+
+def processxls(FILE):
+  BASE_DIR = os.path.dirname(os.path.dirname(__file__))
+  xlsname = os.path.join(BASE_DIR, 'uploads', '%s.xls' % str(uuid.uuid4()))
+  destination = open(xlsname,'wb')
+  for chunk in FILE.chunks(): 
+      destination.write(chunk)
+  destination.close()
+  data = xlrd.open_workbook(xlsname)
+  table = data.sheet_by_index(0)
+  head = table.row_values(0)
+  assert len(head)==6
+  assert head[0].strip()==u'\u5b66\u53f7'
+  assert head[1].strip()==u'\u59d3\u540d'
+  assert head[2].strip()==u'\u6027\u522b'
+  assert head[3].strip()==u'\u5bc6\u7801'
+  assert head[4].strip()==u'\u5206\u7ec4\u53f7'
+  assert head[5].strip()==u'\u751f\u65e5'
+
+
+
+  rets=[]
+  for i in range(table.nrows-1):
+    col = table.row_values(i+1)
+    sid=str(int(col[0]))
+    name=process_cell(table,i+1,1)
+    sex=col[2]
+    password=process_cell(table,i+1,3)
+    cid=str(int(col[4]))
+
+    if table.cell(i+1,5).ctype==3:
+      birthday = datetime.date(*xlrd.xldate_as_tuple(table.cell(i+1,5).value,data.datemode)[:3])
+    else:
+      try:
+        birthday = datetime.datetime.strptime(process_cell(table,i+1,5), "%Y-%m-%d").date()
+      except:
+        try:
+          birthday = datetime.datetime.strptime(process_cell(table,i+1,5), "%Y%m%d").date()
+        except:
+          pass
+          #raise "Error xls!"
+    rets.append((sid,name,sex,password,cid,birthday))
+  return rets
+