def process_register(request): """Register a new user""" c = {} c.update(csrf(request)) if request.method == 'POST': form = UserRegForm(request.POST) if form.is_valid(): new_user = form.save() # Create a default shelf for the user shelf = Shelf( user=new_user, name='Misc', description="%s's miscellaneous shelf" % new_user.username, is_private=False, ) shelf.save() # Log the user in supplied_username = request.POST.get('username', '') supplied_password = request.POST.get('password', '') user = auth.authenticate(username=supplied_username, password=supplied_password) auth.login(request, user) return HttpResponseRedirect( reverse('user_home', args=[user.username])) else: c.update({'form': form}) return render_to_response('register.html', c) else: form = UserRegForm() c.update({'form': form}) return render_to_response("register.html", c)
def process_register(request): """Register a new user""" c = {} c.update(csrf(request)) if request.method == 'POST': form = UserRegForm(request.POST) if form.is_valid(): new_user = form.save() # Create a default shelf for the user shelf = Shelf( user= new_user, name='Misc', description= "%s's miscellaneous shelf" % new_user.username, is_private=False, ) shelf.save() # Log the user in supplied_username = request.POST.get('username', '') supplied_password = request.POST.get('password', '') user = auth.authenticate(username=supplied_username, password=supplied_password) auth.login(request, user) return HttpResponseRedirect(reverse('user_home', args=[user.username])) else: c.update({'form': form}) return render_to_response('register.html', c) else: form = UserRegForm() c.update({'form': form}) return render_to_response("register.html", c)
def api_shelf_create(request): if request.method == 'POST' and request.user.is_authenticated(): form = NewShelfForm(request.POST) if form.is_valid(): try: shelf = Shelf( user=request.user, name=form.cleaned_data['name'], description=form.cleaned_data['description'], is_private=form.cleaned_data['is_private'], ) shelf.save() except ValidationError, e: return HttpResponse('A shelf by that name already exists.', status=409) else: return HttpResponse('Shelf name is required.', status=400) serialized = _serialize_shelf(shelf) return HttpResponse( json.dumps(serialized, cls=DjangoJSONEncoder), mimetype='application/json', status=201, )
def user_create(request): if not request.user.is_authenticated(): messages.warning(request, 'You need to sign in to add items.') #return redirect(reverse('process_login')) return redirect_to_login(urllib.quote_plus(request.get_full_path())) context = {} context.update(csrf(request)) if request.method == 'GET': add_item_form = AddItemForm(request.user) creator_form = CreatorForm() fill_with_get(add_item_form, request.GET) fill_with_get(creator_form, request.GET) elif request.method == 'POST': # A user can create a new shelf while adding an item if 'new_shelf_name' in request.POST: new_shelf = Shelf( user=request.user, name = request.POST['new_shelf_name'], description = request.POST['new_shelf_description'], is_private = request.POST['new_shelf_is_private'], ) try: new_shelf.save() except ValidationError: messages.error(request, 'A shelf with that name already exists.') return redirect(request.path) add_item_form = AddItemForm(request.user, request.POST) creator_form = CreatorForm(request.POST) if add_item_form.is_valid() and creator_form.is_valid(): item = add_item_form.save() _save_creators(creator_form, item) success_text = '%(item)s added to %(shelf)s.' % { 'item': add_item_form.cleaned_data['title'], 'shelf': add_item_form.cleaned_data['shelf'].name } messages.success(request, success_text) return redirect(reverse( 'user_shelf', args=[request.user.username, add_item_form.cleaned_data['shelf'].slug], )) context.update({ 'messages': messages.get_messages(request), 'user': request.user, 'add_item_form': add_item_form, 'creator_form': creator_form }) return render_to_response('item/create.html', context)
def user_home(request, user_name): """A user's home. Includes profile and list of shelves.""" context = _get_user_data(request, user_name) context.update(csrf(request)) context.update({ 'user': request.user, 'new_shelf_form': NewShelfForm() }) context.update({ 'profileform': EditProfileForm(context)}) # Modify user profile if request.rfc5789_method in ['PUT', 'PATCH', 'POST']: profileform = EditProfileForm(request.POST) if not request.user.is_authenticated(): return HttpResponse(status=401) elif user_name != request.user.username: return HttpResponse(status=403) if profileform.is_valid(): _update_user_data(user_name, request.POST) return redirect(reverse('user_home', args=[user_name])) context['profileform'] = profileform # User creates new shelf elif request.method == 'POST': new_shelf_form = NewShelfForm(request.POST) if new_shelf_form.is_valid(): new_shelf = Shelf( user=request.user, name=new_shelf_form.cleaned_data['name'], description=new_shelf_form.cleaned_data['description'], is_private=new_shelf_form.cleaned_data['is_private'], ) try: new_shelf.save() except ValidationError: messages.error(request, 'A shelf with that name already exists.') return redirect(request.path) context['new_shelf_form'] = new_shelf_form context['messages'] = messages.get_messages(request); return render_to_response('user/show.html', context)
def api_shelf_create(request): if request.method == 'POST' and request.user.is_authenticated(): form = NewShelfForm(request.POST) if form.is_valid(): try: shelf = Shelf( user=request.user, name=form.cleaned_data['name'], description=form.cleaned_data['description'], is_private=form.cleaned_data['is_private'], ) shelf.save() except ValidationError, e: return HttpResponse('A shelf by that name already exists.', status=409) else: return HttpResponse('Shelf name is required.', status=400) serialized = _serialize_shelf(shelf) return HttpResponse(json.dumps(serialized, cls=DjangoJSONEncoder), mimetype='application/json', status=201,)
def user_create(request): if not request.user.is_authenticated(): messages.warning(request, 'You need to sign in to add items.') #return redirect(reverse('process_login')) return redirect_to_login(urllib.quote_plus(request.get_full_path())) context = {} context.update(csrf(request)) if request.method == 'GET': add_item_form = AddItemForm(request.user) creator_form = CreatorForm() fill_with_get(add_item_form, request.GET) fill_with_get(creator_form, request.GET) elif request.method == 'POST': # A user can create a new shelf while adding an item if 'new_shelf_name' in request.POST: new_shelf = Shelf( user=request.user, name=request.POST['new_shelf_name'], description=request.POST['new_shelf_description'], is_private=request.POST['new_shelf_is_private'], ) try: new_shelf.save() except ValidationError: messages.error(request, 'A shelf with that name already exists.') return redirect(request.path) add_item_form = AddItemForm(request.user, request.POST) creator_form = CreatorForm(request.POST) if add_item_form.is_valid() and creator_form.is_valid(): item = add_item_form.save() _save_creators(creator_form, item) success_text = '%(item)s added to %(shelf)s.' % { 'item': add_item_form.cleaned_data['title'], 'shelf': add_item_form.cleaned_data['shelf'].name } messages.success(request, success_text) return redirect( reverse( 'user_shelf', args=[ request.user.username, add_item_form.cleaned_data['shelf'].slug ], )) context.update({ 'messages': messages.get_messages(request), 'user': request.user, 'add_item_form': add_item_form, 'creator_form': creator_form }) return render_to_response('item/create.html', context)