Ticket #5584: user_creation.py

File user_creation.py, 13.4 KB (added by benedict.verheyen@…, 17 years ago)

html file containing the traces of the error

Line 
1
2# Programma om de gebruikers afkortingen te maken
3# 18/jul/2007 15:44 Benedict
4# We krijgen sinds de svn update van Django naar versie 5722 de volgende fout op strings
5# Exception unknown encoding: cp0
6# Strings expliciet casten naar str() lost dat probleem op.
7# Het probleem deed zich niet voor als 't programma via de console liep
8# Versie 5600 bevat de merging van de unicode tree
9
10import random
11import logging
12logger = logging.getLogger('user_creation')
13
14class NameException(Exception):
15 pass
16
17class UserCreation(object):
18 """
19 """
20 EXCEPTION_1 = ["Van den", "Van der", "Vander", "Vanden", "Van de", "Vande"]
21 EXCEPTION_Y = ["ey", "ay", "oy"]
22 EXCEPTION_2 = ["Ver", "Den", "Van"]
23 VOWEL = ["a","e","i","o","u","y"]
24 CONSENANT = [chr(char) for char in xrange(ord("a"),ord("z")+1) if chr(char) not in VOWEL ]
25
26 def __init__(self):
27 """
28 We need at least the voornaam & achternaam
29 """
30 self.voornaam = None
31 self.achternaam = None
32
33 def create_all(self, ask_input=False):
34 """
35 @param ask_input: Ask for the name on the prompt or assume the names have been set
36 @type ask_input: boolean
37 """
38 if ( ask_input == True ):
39 self.read_data()
40
41 if ( self.voornaam != None and self.achternaam != None ):
42 self.clean_names()
43 self.create_initials()
44 self.create_email()
45 self.create_logins()
46 self.create_password()
47 self.report()
48 else:
49 print "Geen geldige voor -en/of achternaam"
50
51 def read_data(self):
52 """
53 Read voornaam & achternaam from the prompt
54 """
55 self.voornaam = raw_input("Geef de gebruik voornaam: ")
56 if ( self.voornaam == None or len(self.voornaam) < 1 ):
57 raise NameException("De gebruiker voornaam moet ingevuld worden en langer zijn dan 1 karakter")
58
59 self.achternaam = raw_input("Geef de gebruik achternaam: ")
60 if ( self.achternaam == None or len(self.achternaam) < 1 ):
61 raise NameException("De gebruiker achternaam moet ingevuld worden en langer zijn dan 1 karakter")
62
63 def clean_names(self):
64 """
65 Strip spaces and tabs from the names
66 """
67 self.voornaam = self.voornaam.strip()
68 self.achternaam = self.achternaam.strip()
69
70 self.voornaam_clean = self.voornaam.lower()
71 self.voornaam_clean = ''.join(self.voornaam_clean.split())
72
73 self.achternaam_clean = self.achternaam.lower()
74 self.achternaam_clean = ''.join(self.achternaam_clean.split())
75
76 def create_email(self):
77 """
78 Compose an email adres
79 """
80 try:
81 self.email = "%s.%s@vdl.be" % (self.voornaam_clean, self.achternaam_clean)
82 self.email = self.email.strip().lower()
83 except Exception, e:
84 self.email = ""
85
86 def create_logins(self):
87 """
88 Make the logons for the system and the internet
89 """
90 try:
91 self.login_systeem = "%s.%s" % (self.voornaam_clean, self.achternaam_clean)
92 self.login_internet = "%s%s" % (self.voornaam_clean, self.achternaam_clean)
93 except Exception, e:
94 self.login_systeem = ""
95 self.login_internet = ""
96
97 def _create_exception_1(self, naam):
98 """
99 Exception 1: Name starts with 1 of these: EXCEPTION_1
100 @param naam: The EXCEPTION_1 the lastname starts with
101 @type naam: string
102 """
103 try:
104 #print "Uitzondering 1: Naam %s start met %s." % (str(self.achternaam), str(naam))
105 first = self.voornaam[:1].upper()
106 mid = "VD"
107 last = self.achternaam_clean[len(naam):].strip()[:1].upper()
108 except Exception, e:
109 print "Error in _create_exception_1. Exception %s." % str(e)
110 first = last = "-"
111 mid = "--"
112 return first + mid + last
113
114 def _create_exception_2(self, password_chars):
115 """
116 Exception 2: name start with a string from EXCEPTION_2
117 """
118 try:
119 one, two, four = password_chars
120 for naam in UserCreation.EXCEPTION_2:
121 naam = naam.lower()
122 if ( self.achternaam_clean.startswith(naam) ):
123 if ( naam != "den" or
124 naam == "den" and (self.achternaam_clean[len("den")] in UserCreation.CONSENANT ) ):
125 print "Uitzondering 2: Naam %s start met %s." % (str(self.achternaam), str(naam))
126 three = self.achternaam_clean[len(naam):].strip()[:1].upper()
127 self.initialen = str(one + two + three + four)
128 return
129 except Exception, e:
130 print "Error in _create_exception_2. Exception %s." % str(e)
131 logger.debug("Error in _create_exception_2. Exception %s." % str(e))
132 self.initialen = "----"
133
134 def _create_exception_3(self, password_chars):
135 """
136 Exception 3: name starts with a vowel
137 """
138 try:
139 one, two, four = password_chars
140 for klinker in UserCreation.VOWEL:
141 if ( self.achternaam_clean.startswith(klinker) ):
142 print "Uitzondering 3: Naam %s start met %s." % (str(self.achternaam), str(klinker))
143 #print "Achternaam start met klinker %s " % klinker
144 for letter in self.achternaam_clean:
145 if ( letter in UserCreation.CONSENANT ):
146 three = letter.upper()
147 self.initialen = str(one + two + three + four)
148 return
149 except Exception, e:
150 print "Error in _create_exception_3. Exception %s." % str(e)
151 logger.debug("Error in _create_exception_3. Exception %s." % str(e))
152 self.initialen = "----"
153
154 def _create_exception_4(self, password_chars):
155 """
156 Exception 4: No consenant between 1st letter and last consenant
157 """
158 try:
159 consenant = False
160 laatste_consenant = self._find_last_cons(self.achternaam_clean)
161 in_between = self.achternaam_clean[1:laatste_consenant-1]
162 one, two, four = password_chars
163 for letter in in_between:
164 if ( letter in UserCreation.CONSENANT ):
165 consenant = True
166 break
167 if ( consenant == False ):
168 three = self.achternaam_clean[1].upper()
169 print "Uitzondering 4: Naam %s." % str(self.achternaam)
170 self.initialen = str(one + two + three + four)
171 return
172 except Exception, e:
173 print "Error in _create_exception_4. Exception %s." % str(e)
174 logger.debug("Error in _create_exception_4. Exception %s." % str(e))
175 self.initialen = "----"
176
177 def create_password(self):
178 """
179 Create a random password for the user
180 """
181 try:
182 # we need at least 5 chars
183 # in case of names like Harm-Wouter, remove the -
184 #
185 voornaam = (self.voornaam[:5].lower()).replace("-","")
186 self.paswoord = voornaam
187 length_voornaam = len(voornaam)
188 if ( length_voornaam < 5 ):
189 still_needed = 5 - length_voornaam
190 self.paswoord += self.achternaam_clean[:still_needed]
191 if ( len(self.paswoord) < 5 ):
192 raise Exception("Ik kan geen 5 karakters gebruiken voor de naam omdat ze te kort zijn.")
193 numbers = [str(random.randint(0,9)) for nr in xrange(0,6) ]
194 self.paswoord += "".join(numbers)
195 except Exception, e:
196 print "Error in create_password. Exception %s." % str(e)
197 self.paswoord = "UNDEFINED"
198 return self.paswoord
199
200 def _y_is_consonant(self):
201 """
202 Remove y from VOWEL and add to CONSENANT
203 """
204 try:
205 UserCreation.VOWEL = UserCreation.VOWEL[:-1]
206 UserCreation.CONSENANT.insert(len(UserCreation.CONSENANT)-1,"y")
207 except Exception, e:
208 raise Exception("_y_is_consonant problem: %s" % str(e) )
209
210 def _find_last_cons(self,search):
211 """
212 Find the last consenant
213 """
214 try:
215 pos = len(search)
216 for letter in search[::-1].lower():
217 if letter in UserCreation.CONSENANT:
218 return pos
219 pos = pos-1
220 except Exception, e:
221 raise Exception("_find_last_cons problem: %s" % str(e) )
222 return pos
223
224 def _find_second_cons(self,search):
225 """
226 Find the 2nd last consenant
227 """
228 pos = 0
229 nr = 0
230 try:
231 for letter in search.lower():
232 if letter in UserCreation.CONSENANT:
233 nr+=1
234 if ( nr == 2 ): break
235 pos += 1
236 except Exception, e:
237 raise Exception("_find_second_cons problem: %s" % str(e) )
238 return pos
239
240 def _is_y_consenant(self):
241 """
242 Find out if there are combinations with y that force y to be a consenant
243 """
244 try:
245 for letter_combo in UserCreation.EXCEPTION_Y:
246 #print "Zoeken %s in %s " % (letter_combo, self.achternaam_clean)
247 if (self.achternaam_clean.find(letter_combo) != -1):
248 #print "%s gevonden. y is een CONSENANT" % letter_combo
249 self._y_is_consonant()
250 break
251 except Exception, e:
252 raise Exception("_is_y_consenant problem: %s" % str(e) )
253
254 def create_initials(self):
255 """
256 Create initials
257 """
258 self.initialen = ""
259
260 UserCreation.VOWEL = ["a","e","i","o","u","y"]
261 UserCreation.CONSENANT = [chr(char) for char in xrange(ord("a"),ord("z")+1) if chr(char) not in UserCreation.VOWEL ]
262
263 # Uitzondering 1
264 # Achternaam start met Van
265 # Creatie volledig paswoord
266 try:
267 for naam in UserCreation.EXCEPTION_1:
268 naam = naam.lower()
269 if ( self.achternaam_clean.startswith(naam) ):
270 print "Uitzondering 1: Naam %s start met %s." % (str(self.achternaam),str(naam))
271 self.initialen = self._create_exception_1(naam)
272 return
273 except Exception, e:
274 print "Error in exception 1. Exception %s." % str(e)
275 logger.debug("Error in exception 1. Exception %s." % str(e))
276 self.initialen = "----"
277
278 # Detectie of we een y hebben die we als CONSENANT of klinker beschouwen
279 self._is_y_consenant()
280
281 #print "CONSENANT ",str(UserCreation.CONSENANT)
282 #print "VOWEL ",str(UserCreation.VOWEL)
283
284 # Kies de 1ste, 2de & 4de letter
285 one = self.voornaam[:1].upper()
286 two = self.achternaam[:1].upper()
287 four = ""
288 for letter in self.achternaam_clean[::-1]:
289 if letter in UserCreation.CONSENANT:
290 four = letter.upper()
291 #print "Laatste CONSENANT achternaam %s" % letter
292 break
293
294 password_chars = [one, two, four]
295
296 # Uitzondering 2
297 # Achternaam start met Van, Den (gevolgd door CONSENANT) of Ver
298 # Standaard, enkel de 3e verschilt
299 self._create_exception_2(password_chars)
300 if ( len(self.initialen) == 4 ): return
301
302 # Uitzondering 3
303 # Achternaam start met een klinker
304 # Standaard, enkel de 3e verschilt
305 self._create_exception_3(password_chars)
306 if ( len(self.initialen) == 4 ): return
307
308 # Uitzondering 4
309 # Achternaam heeft GEEN CONSENANT tussen 1ste letter en de laatste CONSENANT
310 # Standaard, enkel de 3e verschilt
311 self._create_exception_4(password_chars)
312 if ( len(self.initialen) == 4 ): return
313
314 # Normaal geval
315 print "Geen uitzondering gevonden voor %s %s " % (str(self.voornaam),str(self.achternaam))
316 #print "CONSENANT %s " % str(UserCreation.CONSENANT)
317 three = self.achternaam_clean[self._find_second_cons(self.achternaam_clean)].upper()
318 self.initialen = str(one + two + three + four)
319 return self.initialen
320
321 def report(self):
322 """
323 """
324 print " "
325 print " "
326 print "Emailen naar Hein & de Office manager"
327 print "-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<"
328
329 a = unicode('Paswoord: %s ' % self.paswoord, 'ascii')
330 print a
331 #print u"Paswoord: %s " % self.paswoord
332
333 print "-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<"
334 print " "
335 print " "
336 print "Emailen naar Hein, Martin, Barbara"
337 print "-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<"
338 print "Initialen: %s " % str(self.initialen)
339 print "Login systeem: %s " % str(self.login_systeem)
340 print "Login internet/surfen: %s " % str(self.login_internet)
341 print "Email: %s " % str(self.email)
342 print "CCS nummer: "
343 print "Telefoon nummer: "
344 print "-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<"
345
346if __name__=="__main__":
347 u = UserCreation()
348 u.create_all(True)
Back to Top