Ejemplo n.º 1
0
def register(request):
	context = RequestContext(request)
	registered = False
	if request.method == 'POST':
		user_form = UserForm(request.POST, prefix='user')
		user_profile_form = UserProfileForm(request.POST, prefix='profile')
		cc_form = CreditCardForm(request.POST, prefix='cc')
		address_form = AddressForm(request.POST, prefix='address')
		if user_form.is_valid() and user_profile_form.is_valid() and cc_form.is_valid() and address_form.is_valid():
			user_model = user_form.save()
			cc_model = cc_form.save()
			address_model = address_form.save()
			user_model.set_password(user_model.password)
			user_model.save()
			user_profile_model = user_profile_form.save(commit=False)
			user_profile_model.user = user_model
			user_profile_model.creditCard = cc_model
			user_profile_model.address = address_model
			if 'picture' in request.FILES:
				profile.picture = request.FILES['picture']
			user_profile_model.save()
			registered = True
		else:
			print user_form.errors, user_profile_form.errors, cc_form.errors, address_form.errors
	else:
		user_form = UserForm(prefix='user')
		user_profile_form = UserProfileForm(prefix='profile')
		cc_form = CreditCardForm(prefix='cc')
		address_form = AddressForm(prefix='address')
	return render_to_response('web/register.html',{'user_form': user_form, 'user_profile_form': user_profile_form, 'cc_form': cc_form, 'address_form': address_form, 'registered': registered}, context)
Ejemplo n.º 2
0
def register(request):
	if request.method == 'GET':
		#redirect to home if already logged in
		auth = request.COOKIES.get('auth')
		#check whether user is logged in
		if not auth:
			logged_in = False
		else:
			resp = checkauth_exp_api(auth)
			if (resp["ok"] == True):
				user_id = resp["resp"]["user_id"]
				logged_in = True
				return HttpResponseRedirect(reverse("home"))
		vendor_form = VendorForm()
		user_form = UserForm()
		next = request.GET.get('next') or reverse('home')
		return render(request, 'register.html', {'next':next, 'user_form': user_form, 'vendor_form': vendor_form})
	user_form = UserForm(request.POST)
	vendor_form = VendorForm(request.POST)
	if not user_form.is_valid():
		return render(request, "register.html", {'state': "Invalid Input", 'user_form': user_form, "vendor_form": vendor_form})
	username = user_form.cleaned_data['username']
	email = user_form.cleaned_data['email']
	f_name = user_form.cleaned_data['first_name']
	l_name = user_form.cleaned_data['last_name']
	password = user_form.cleaned_data['password']
	is_vendor = user_form.cleaned_data['is_vendor']
	vendor_name = ""
	if (is_vendor == True):
		if not vendor_form.is_valid():
			expire_view_cache(request, 'register')
			return render(request, "register.html", \
						  {'state': "Vendors must fill out company name", \
						   'user_form': user_form, "vendor_form": vendor_form})
		vendor_name = vendor_form.cleaned_data['vendor_name']
	post_data = {'username': username,      \
                                    	'email': email,           \
                                       'f_name': f_name,          \
                                       'l_name': l_name,          \
                                       'password': password,  \
				 						'is_vendor': is_vendor, \
				 						'vendor_name': vendor_name  \
                                       }
	post_encoded = urllib.parse.urlencode(post_data).encode('utf-8')
	req = urllib.request.Request('http://' + settings.EXP_API + ':8000/api/register/', data=post_encoded, method='POST')
	resp_json = urllib.request.urlopen(req).read().decode('utf-8')
	resp = json.loads(resp_json)
	if (resp['ok'] == False):
		expire_view_cache(request, 'register')
		return render(request, "register.html", {'state': "Unsucessful registration", 'user_form': user_form, 'vendor_form':vendor_form})
	expire_view_cache(request, 'login')
	if (is_vendor == True):
		expire_view_cache(request, 'home')
	return HttpResponseRedirect(reverse('login'), {'status': 'Registration sucessful. Please login.'})
Ejemplo n.º 3
0
    def test_user_save_method(self):
        from web.forms import UserForm
        form = UserForm(data={
            'username': '******',
            'password': '******'
        })

        self.assertEqual(form.is_valid(), True)

        response = form.save()
        self.assertEqual(response.status_code, 201)
Ejemplo n.º 4
0
def change_user(request,pk=None):
   user = User.objects.filter(pk=pk).first()
   form_obj = UserForm(instance=user)
   title="编辑" if pk else "添加"
   if request.method == 'POST':
      form_obj = UserForm(request.POST,instance=user)
      if form_obj.is_valid():
         form_obj.save()
         return JsonResponse({'status':0,'msg':f'{title}成功'})
      else:
         return JsonResponse({'status':1,'msg':f'{title}失败,失败的原因是{form_obj.errors}'})
   return render(request, 'create/user_create.html', {'form_obj':form_obj, "pk":pk})
Ejemplo n.º 5
0
 def post(self, request):
     form = UserForm(request.POST)
     if form.is_valid():
         new_user = form.save(
         )  # We are gonna save the object and it will be back
         messages.add_message(request, messages.INFO,
                              _('You have been registered succesfully!'))
         return redirect('login')
     context = {
         'form': form,
     }
     return render(request, 'user/signup.html', context)
Ejemplo n.º 6
0
def index(request):

    if request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/')

    else:
        form = UserForm()

    return render(request, 'index.html', {'form': form})
Ejemplo n.º 7
0
def signUpView(request):
    if request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            create_account = AccountService.CreateAccount(username, raw_password)
            messages.add_message(request, messages.INFO, 'Account created with success', 'alert-success')
            return redirect('login')
    else:
        form = UserForm()
    return render(request, 'sign_up.html', {'form': form})
Ejemplo n.º 8
0
def uploads(request):
    if request.method == 'POST':
        userform = UserForm(request.POST, request.FILES)
        # print(userform)
        if userform.is_valid():
            fileinstall = File()
            fileinstall.fileName = userform.cleaned_data['fileName']
            fileinstall.uploadFile = userform.cleaned_data['uploadFile']
            fileinstall.save()
            return render_to_response('uploadOK.html')
    else:
        userform = UserForm(initial ={'fileName': 'sunshore'})
    return render_to_response('upload.html', {'userform': userform})
Ejemplo n.º 9
0
class UserFormTest(TestCase):
    def setUp(self):
        self.form = UserForm(data={
            'username': '******',
            'password': '******'
        })

    def tearDown(self):
        self.client = Client()

    def test_valid_form(self):
        self.assertTrue(self.form.is_valid())

    def test_invalid_form(self):
        form = UserForm(data={'email': "", 'password': ""})
        self.assertFalse(form.is_valid())
Ejemplo n.º 10
0
def register(request):
    registered = False
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            try:
                user = user_form.save()
                user.set_password(user.password)
                user.save()
                profile = profile_form.save(commit=False)
                profile.user = user
                profile.save()
                registered = True  # Invalid form or forms - mistakes or something else?
                username = user_form.cleaned_data.get('username')
                raw_password = user_form.cleaned_data.get('password')
                user = authenticate(username=username, password=raw_password)
                login(request, user)
            except Exception as e:
                messages.add_message(request, messages.ERROR, e)
            return redirect('/accounts/dashboard/')
        else:
            messages.add_message(request, messages.ERROR, user_form.errors)
            redirect('/home/')
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
        user_form.fields['first_name'].widget.attrs.update(
            {'placeholder': 'FirstName'})
        user_form.fields['first_name'].widget.attrs.update({'tabindex': 0})
        user_form.fields['last_name'].widget.attrs.update(
            {'placeholder': 'LastName'})
        user_form.fields['username'].widget.attrs.update(
            {'placeholder': 'UserName'})
        profile_form.fields['phone_number'].widget.attrs.update(
            {'placeholder': 'PhoneNumber Ex: +98xxxxxxx'})
        user_form.fields['email'].widget.attrs.update(
            {'placeholder': 'Email Ex: [email protected]'})
        user_form.fields['password'].widget.attrs.update(
            {'placeholder': 'Password'})
        return render(request,
                      'signup.html',
                      context={
                          'user_form': user_form,
                          'profile_form': profile_form,
                          'registered': registered
                      })
Ejemplo n.º 11
0
def register(request):
    # information from www.tangowithdjango.com
    context = RequestContext(request)
    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        if user_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            registered = True
        else:
            print user_form.errors
    else:
        user_form = UserForm()
        return render_to_response()
Ejemplo n.º 12
0
def register(request):
    # information from www.tangowithdjango.com
    context = RequestContext(request)
    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        if user_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            registered = True
        else:
            print user_form.errors
    else:
        user_form = UserForm()
        return render_to_response()
Ejemplo n.º 13
0
def register(request):
    if request.POST:
        user_form = UserForm(request.POST)
        if user_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            email = Emails()
            email.register_me(user.email)
            return redirect('login')
        else:
            messages.add_message(request, messages.INFO,
                                 'Nieprawidłowe dane uwierzytelniające.')

    else:
        user_form = UserForm()

    return render(request, 'account/register.html', {'user_form': user_form})
Ejemplo n.º 14
0
def register(request):
    '''
    '''
    if request.method == 'GET':
        obj = UserForm(request.POST)
        return render(request, 'register.html', {'obj': obj})
    #context = {}
    elif request.method == 'POST':
        ret = {'status': False, 'error': None, 'data': None}
        form = UserForm(request.POST)
        if form.is_valid():
            #获得表单数据
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            email = form.cleaned_data['email']

            # 判断用户是否存在
            user = auth.authenticate(username=username, password=password)

            if not user:
                #添加到数据库(还可以加一些字段的处理)
                user = User.objects.create_user(username=username,
                                                email=email,
                                                password=password)
                user.save()
                usernames = models.UserProfile.objects.create(user_id=user.id,
                                                              name=username)
                usernames.save()
                #添加到session
                request.session['username'] = usernames.name
                #调用auth登录
                login(request, user)
                ret['status'] = True
                ret['data'] = form.cleaned_data
                ret = json.dumps(ret, cls=JsonCustomEncoder)
            else:
                ret['error'] = {'user': '******'}
                ret = json.dumps(ret, cls=JsonCustomEncoder)

        else:
            ret['error'] = form.errors.as_data()
            ret = json.dumps(ret, cls=JsonCustomEncoder)

        return HttpResponse(ret)
Ejemplo n.º 15
0
def signup(request):

  context = RequestContext(request) # get last inputs

  registered = False

  if request.method == 'POST':
    # POST Method for save userdata to database
    user_form = UserForm(data = request.POST)
    profile_form = UserProfileForm(data = request.POST)

    if user_form.is_valid() and profile_form.is_valid():

      user = user_form.save()

      user.set_password(user.password)
      user.save()   # save user to database

      profile = profile_form.save(commit=False)
      profile.user = user

      profile.save()

      registered = True

      return HttpResponseRedirect(reverse('login'))

    else:
      print user_form.errors, profile_form.errors




  else:
    # Create User Form
    user_form = UserForm()
    profile_form = UserProfileForm()

    # Return to the same page
  return render_to_response(
      'signup.html',
      {'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
      context)
Ejemplo n.º 16
0
def register(request):
    users = CustomUser.objects.top_5()
    tags = Tag.objects.popular()
    if request.method == "POST":
        form = UserForm(request.POST, request.FILES)
        if form.is_valid():
            user = form.save()
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            auth_login(request, user)
            return redirect("home")
    else:
        form = UserForm()
    context = {
        "auth": request.user,
        "form": form,
        "users": users,
        "tags": tags,
    }
    return render(request, "web/register.html", context)
Ejemplo n.º 17
0
 def test_invalid_form(self):
     form = UserForm(data={'email': "", 'password': ""})
     self.assertFalse(form.is_valid())