/
register_epmv.py
287 lines (262 loc) · 9.99 KB
/
register_epmv.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""
Copyright (C) <2010> Autin L.
This file ePMV_git/register_epmv.py is part of ePMV.
ePMV is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ePMV 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with ePMV. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
"""
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 7 07:14:08 2011
@author: -
"""
import os
import sys
import socket
try :
#python 3
import urllib.parse as urllib
from http import client as httplib
except :
import urllib
import httplib
import pickle
import time
import shutil
from mglutil.util.packageFilePath import getResourceFolderWithVersion
from mglutil.util.packageFilePath import getResourceFolder
try :
from upy import uiadaptor
if uiadaptor is None :
uiadaptor = upy.getUIClass()
except :
uiadaptor = None
keyword = {"First Name*":"",
"Last Name*":"",
"Email*":"",
"Institution*":"",
"Institution Type*":"",
"Position*":' ',
"Department":"",
"Address":"",
"City":"",
"State":"",
"PostalCode":"",
"Country":"",
"Phone":"",
"Fax":"",}
order = ["First Name*","Last Name*","Email*","Institution*","Institution Type*","Position*",
"Department","Address", "City","State","PostalCode", "Country", "Phone",
"Fax",]
dicToSend = {'sys_version':'',
'City': '',
'First_Name': '',
'Last_Name': '',
'Phone': '',
'Country': '',
'hostname': '',
'PlanningToUse': ',PMV',
'Fax': '',
'Institution': '',
'PostalCode': '',
'State': '',
'version': '',
'Institution_Type': 'Academic',
'BuildFrom':'Binary',
'Address': '',
'Department': '',
'Position': '',
'sys_platfrom': '',
'Email': '',
'os_name': '',
'3DHost':''}
class Register_User_ePMV:
"""Opens TopLevel Dialog for User Registration"""
def __init__(self,use="ePMV"):
self.form_dict = keyword.copy()
from Support.version import __version__
self.sys_dict = {
'PlanningToUse':use,
'BuildFrom':'Binary',
'version':__version__.split('(')[0],
'os_name': os.name,
'sys_platfrom':sys.platform,
'sys_version':sys.version.replace('\n','')#.split('(')[0],
#'UserID':'',
}
self.form_dict.update(self.sys_dict)
try:
hostname = self.gethostname()
self.form_dict['hostname'] = hostname
except:
self.form_dict['hostname'] = "problem"
self.form_dict['3DHost'] = ""
self.preFill()
def register(self,):
self.dictToSend={}
for i,k in enumerate(order) :
dk = k
if i <=5 :
dk = dk.replace("*","")
dk = dk.replace(" ","_")
self.dictToSend[dk] = str(self.form_dict[k]).strip()
#print (dk,self.dictToSend[dk])
self.dictToSend.update(self.sys_dict)
self.dictToSend['hostname'] = self.form_dict['hostname']
self.dictToSend['3DHost'] = self.form_dict['3DHost']
# for k in self.dictToSend:
# print k, self.dictToSend[k]
params = urllib.urlencode(self.dictToSend)
#print (params)
#self.label_message.configure(text = 'Please Wait', fg = 'Red')
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
# conn = httplib.HTTPConnection("www.scripps.edu:80")
conn = httplib.HTTPConnection("mgldev.scripps.edu:80")
try:
# conn.request("POST", "/cgi-bin/sanner/register_mgltools.py", params, headers)
conn.request("POST", "/cgi-bin/register_mgltools.py", params, headers)
response = conn.getresponse()
except :
# from traceback import print_exc
# print_exc()
print ("fail to connect")
return False
if response.status == 200:
# getResourceFolder
reg_file = os.path.join(getResourceFolder(), '.registration')
# print (reg_file)
UserID = response.read().decode("utf8")
# print (UserID)
if UserID:
self.form_dict['UserID'] = UserID
file = open(reg_file,'wb')
pickle.dump(self.form_dict, file)
file.close()
c_reg_file = os.path.join(getResourceFolderWithVersion(), '.registration')
shutil.copy(reg_file, c_reg_file)
else:
print ("Registration failed to create User.")
return False
else:
print ("Unable to connect to Registration Database" )
return False
conn.close()
return True
def preFill(self):
old_rc = getResourceFolder()
regfile = os.path.join(old_rc, ".registration")
if os.path.exists(regfile):
try :
form_dict = pickle.load(open(regfile, 'rb'))
except :
return
print(form_dict)
for i,k in enumerate(order) :
dk = k
if i <=5 :
if dk not in form_dict :
dk = dk.replace("*","")
dk = dk.replace(" ","_")
self.form_dict[k] = str(form_dict[dk]).strip()
def gethostname(self):
fullname = socket.gethostname()
if '.' not in fullname:
fullname = resolve(fullname)
return fullname
class Register_User_ePMV_ui (uiadaptor) :
def setup(self,sub=True,r=None,id=2000,use="ePMV"):
self.h = 350
self.w = 300
self.subdialog = sub
self.title = "Register ePMV"
self.SetTitle(self.title)
if self.subdialog:
self.block = True
witdh=350
if id is not None :
id=id
else:
id = self.bid
if r is None :
self.reg = Register_User_ePMV(use=use)
else :
self.reg = r
self.initWidget()
self.setupLayout()
self.registered = False
return True
def initWidget(self):
self.widget_form = {}
self.label_form = {}
self.liste_institution = ["Academic", "Government", "Commercial"]
for k in order :
if k == "Institution Type*":
self.label_form[k] = self._addElemt(label=k+' Academic, Government, Commercial',
width=120)
#pullDownMenu
self.widget_form[k] = self._addElemt(name=k,
value=self.liste_institution,
width=100,height=10,action=None,
variable=self.addVariable("int",0),
type="pullMenu",)
else :
self.label_form[k] = self._addElemt(label=k,width=120)
self.widget_form[k] = self._addElemt(name=k,width=100,height=10,
action=None,type="inputStr",
value=self.reg.form_dict[k],
variable=self.addVariable("str",""))
self.label_message = self._addElemt(label="Registration Form. * are required",width=120)
self.regButton = self._addElemt(name="Register",width=50,height=10,
action=self.Register,type="button")
self.cancelButton = self._addElemt(name="Cancel",width=50,height=10,
action=self.close,type="button")
def setupLayout(self):
self._layout = []
for k in order :
self._layout.append([self.label_form[k],self.widget_form[k]])
self._layout.append([self.label_message,])
self._layout.append([self.regButton,self.cancelButton])
def checkForm(self):
for i in range(5):
if not self.reg.form_dict[order[i]]:
self.setString(self.label_message,str(order[i]+' is missing or invalid'))
return False
self.setString(self.label_message,'')
return True
def Register(self,*args):
#get the value in the widget, and send
for k in keyword :
self.reg.form_dict[k] = self.getVal(self.widget_form[k])
res = self.checkForm()
print(("check",res))
print((self.reg.form_dict))
if not res :
return
print ('Submitting the Registration Form\nPlease Wait')
self.setVal(self.label_message, 'Submitting the Registration Form\nPlease Wait')
self.reg.form_dict["3DHost"] = self.host
res = self.reg.register()
if not res :
self.drawMessage(title="ERROR",
message="Registration failed to create User.\nPlease contact mgltools@scripps.edu")
self.registered=False
self.close()
else :
self.drawMessage(title="SUCCESS",
message="Registration success\n")
self.registered=True
self.close()
def CreateLayout(self):
self._createLayout()
return True
def Command(self,*args):
# print args
self._command(args)
return True