aboutsummaryrefslogtreecommitdiff
path: root/web/webqtl/misc/uploadFilePage.py
blob: da679910a97997409e465c927cf8e9701e401db2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright (C) University of Tennessee Health Science Center, Memphis, TN.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# This program is available from Source Forge: at GeneNetwork Project
# (sourceforge.net/projects/genenetwork/).
#
# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010)
# at rwilliams@uthsc.edu and xzhou15@uthsc.edu
#
#
#
# This module is used by GeneNetwork project (www.genenetwork.org)
#
# Created by GeneNetwork Core Team 2010/08/10
#
# Last updated by GeneNetwork Core Team 2010/10/20

import os
import string

from htmlgen import HTMLgen2 as HT

from base.templatePage import templatePage
from base import webqtlConfig
from utility import webqtlUtil

#########################################
#      Upload File Page 
#########################################

class uploadFilePage(templatePage):

	uploadPath = webqtlConfig.UPLOADPATH

	def __init__(self, fd, formdata, cookies):

		templatePage.__init__(self, fd)

		if not self.openMysql():
			return

                if webqtlConfig.USERDICT[self.privilege] >= webqtlConfig.USERDICT['user']:
                        pass
                else:
                        heading = "Upload File"
                        detail = ["You don't have the permission to upload file to the server."]
                        self.error(heading=heading,detail=detail)
                        return
		
		self.cursor.close()

		file1 = self.save_uploaded_file (formdata, 'imgName1')
		file2 = self.save_uploaded_file (formdata, 'imgName2')
		file3 = self.save_uploaded_file (formdata, 'imgName3')
		file4 = self.save_uploaded_file (formdata, 'imgName4')
		file5 = self.save_uploaded_file (formdata, 'imgName5')
		
		i = 0
		uploaded = []

		for filename in (file1, file2, file3, file4, file5):
			if filename: 
				i += 1
				uploaded.append(filename)
				
		if i == 0:
			heading = "Upload File"
			detail = ["No file was selected, no file uploaded."]
			self.error(heading=heading,detail=detail)
			return
		else:
			TD_LR = HT.TD(height=200,width="100%",bgColor='#eeeeee')

			imgTbl = HT.TableLite(border=0, width = "90%",cellspacing=2, cellpadding=2, align="Center")
			imgTbl.append(HT.TR(HT.TD("Thumbnail", width="%30", align='center',Class="colorBlue"), 
					HT.TD("URL", width="%60",Class="colorBlue", align='center')))
			for item in uploaded:
				img = HT.Image("/images/upload/" + item, border = 0, width=80)

				#url = "%s/images/upload/" % webqtlConfig.PORTADDR + item
				#url = HT.Href(text=url, url = url, Class='normalsize', target="_blank")
				url2 = "/images/upload/" + item
				url2 = HT.Href(text=url2, url = url2, Class='normalsize', target="_blank")
				imgTbl.append(HT.TR(HT.TD(img, width="%30", align='center',Class="colorWhite"), 
					#HT.TD(url, HT.BR(), 'OR', HT.BR(), url2,  width="%60",Class="colorWhite", align='center')))
					HT.TD(url2,  width="%60",Class="colorWhite", align='center')))

			intro = HT.Paragraph('A total of %d files are uploaded' % i)
			TD_LR.append( HT.Center(intro) )

			TD_LR.append( imgTbl )
		
			self.dict['body'] = str(TD_LR)	
		
	def save_uploaded_file(self, form, form_field, upload_dir=""):
		if not upload_dir:
			upload_dir = self.uploadPath
		if not form.has_key(form_field):
			return None
		fileitem = form[form_field]
		
		if not fileitem.filename or not fileitem.file:
			return None
		
		seqs = [""] + range(200)
		try:
			newfileName =  string.split(fileitem.filename, ".")
			for seq in seqs:
				newfileName2 = newfileName[:]
				if seq != "":
					newfileName2[-2] = "%s-%s" % (newfileName2[-2], seq)
				fileExist = glob.glob(os.path.join(upload_dir, string.join(newfileName2, ".")))
				if not fileExist:
					break
		except:
			pass
		
		newfileName = string.join(newfileName2, ".")
		#print [newfileName, os.path.join(upload_dir, newfileName)]
		#return
		
		fout = file (os.path.join(upload_dir, newfileName), 'wb')
		while 1:
			chunk = fileitem.file.read(100000)
			if not chunk: break
			fout.write (chunk)
		fout.close()
		return newfileName