def create(req): error = [] name = req.POST.get("name").strip() email = req.POST.get("email").strip() username = req.POST.get("username").strip() roll_number = req.POST.get("roll_number").strip() password = req.POST.get("password") confirm = req.POST.get("confirm") institute = req.POST.get("institute").strip() department = req.POST.get("department").strip() position = req.POST.get("position").strip() error = error + (["Please enter a name."] if name == "" else []) error = error + (["Please enter an email."] if email == "" else []) error = error + (["Please enter an username."] if username == "" else []) error = error + (["Please enter a roll_number."] if roll_number == "" else []) error = error + (["Please enter a password."] if password == "" else []) error = error + (["Password confirmation does not match."] if password != confirm else []) error = error + (["Please enter an institute."] if institute == "" else []) error = error + (["Please enter a department."] if department == "" else []) error = error + (["Please enter a position."] if position == "" else []) try: validate_email(email) except ValidationError: error = error + ["Please enter a valid email."] email_exists = Account.objects.filter(email=email).count() error = error + (["Account with given email already exists."] if email_exists > 0 else []) username_exists = Account.objects.filter(username=username).count() error = error + (["Account with given username already exists."] if username_exists > 0 else []) if error != []: messages.add_message(req, messages.ERROR, "<br>".join(error)) return redirect(index) # try: account = Account( name=name, username=username, email=email, board_id=Board.allot_board() ) account.set_password(password) account.save() account.send_confirmation() print "Done" messages.add_message(req, messages.SUCCESS, "You have been registered successfully. Please check your email for confirmation.") return redirect(index)
def create(req): """ Creates a new user account. Generate user alerts if: Any of the fields is not entered. Entered email-id is invalid. Entered username or email-id is existing. Alloted board id is -1, because of no boards being available online. If the user credentials cross all checks and hence a successful account is created. Input: s:request object. Output: HttpResponseRedirect object returned by redirect() function. """ error = [] name = req.POST.get("name").strip() email = req.POST.get("email").strip() username = req.POST.get("username").strip() roll_number = req.POST.get("roll_number").strip() password = req.POST.get("password") confirm = req.POST.get("confirm") institute = req.POST.get("institute").strip() department = req.POST.get("department").strip() position = req.POST.get("position").strip() error = error + (["Please enter a name."] if name == "" else []) error = error + (["Please enter an email."] if email == "" else []) error = error + (["Please enter an username."] if username == "" else []) error = error + (["Please enter a roll_number."] if roll_number == "" else []) error = error + (["Please enter a password."] if password == "" else []) error = error + (["Password confirmation does not match."] if password != confirm else []) error = error + (["Please enter an institute."] if institute == "" else []) error = error + (["Please enter a department."] if department == "" else []) error = error + (["Please enter a position."] if position == "" else []) try: validate_email(email) except ValidationError: error = error + ["Please enter a valid email."] email_exists = Account.objects.filter(email=email).count() error = error + (["Account with given email already exists."] if email_exists > 0 else []) username_exists = Account.objects.filter(username=username).count() error = error + (["Account with given username already exists."] if username_exists > 0 else []) if error != []: messages.add_message(req, messages.ERROR, "<br>".join(error)) return redirect(index) # try: # check if a board could be allocated allocated_board_id = Board.allot_board() if allocated_board_id == -1: messages.add_message( req, messages.ERROR, "Sorry!! No boards online at this moment. Try again in some time.") return redirect(index) account = Account(name=name, username=username, email=email, board_id=allocated_board_id, last_login=datetime.now().strftime("%Y-%m-%d %H:%M")) account.set_password(password) account.save() account.send_confirmation() print "Done" messages.add_message( req, messages.SUCCESS, "You have been registered successfully. Please check your email for confirmation." ) return redirect(index)