def create(request): if request.method!='POST': return HttpResponseRedirect(reverse('manual-index')) new_app_form = NewAppForm(request.POST) if new_app_form.is_valid(): applicant = new_app_form.get_applicant() applicant.random_password(20) # prefix email to prevent e-mail index crash applicant.email = "%s-%s" % (random_string(5),applicant.email) applicant.is_offline = True try: applicant.save() except IntegrityError: # shouldn't happen! set_notice_message(request,'ไม่สามารถสร้างได้เนื่องจากอีเมล์ซ้ำ') return HttpResponseRedirect(reverse('manual-index')) return HttpResponseRedirect(reverse('manual-personal', args=[applicant.id])) set_notice_message(request,'ไม่สามารถสร้างได้เนื่องจากข้อมูลผิดพลาด') return HttpResponseRedirect(reverse('manual-index'))
def populate_upload_field_forms(app_docs, fields, required_fields=None): field_forms = {} for f in fields: field = AppDocs._meta.get_field_by_name(f)[0] has_thumbnail = False if app_docs!=None: field_value = app_docs.__getattribute__(f) if (field_value!=None) and (field_value.name!=''): has_thumbnail = True if (required_fields == None) or (f in required_fields): required = True else: required = False field_forms[f] = { 'name': f, 'field': field, 'required' :required, 'has_thumbnail': has_thumbnail, 'random_string': random_string(5), 'form': FileUploadForm() } return field_forms
def random_activation_key(self): self.activation_key = random_string(10) return self.activation_key
def random_salt(self): self.salt = random_string(10)
def random_password(self, length=5): import random password = random_string(5) self.set_password(password) return password
def random_string(self): try: return self._random_string except: self._random_string = random_string(5) return self._random_string