Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from basic_app.forms import UserProfileInfoForm
- from basic_app.forms import PrestamoForm
- from basic_app.forms import RegEstudianteForm
- # Extra Imports for the Login and Logout Capabilities
- from django.contrib.auth import authenticate, login, logout
- from django.http import HttpResponseRedirect, HttpResponse
- from django.core.urlresolvers import reverse
- from django.contrib.auth.decorators import login_required
- from django.shortcuts import render_to_response
- def index(request):
- return render(request,'basic_app/index.html')
- #def webIApage(request):
- # return render(request, 'basic_app/camera.html')
- @login_required
- def prestamo(request):
- Prest_form = forms.PrestamoForm()
- if request.method == 'POST':
- Prest_form = PrestamoForm(data=request.POST)
- if Prest_form.is_valid():
- Prest_form.save()
- return render(request, 'basic_app/PagPrestamo.html', {'Prest_form':Prest_form})
- @login_required
- def registroEstudiantes(request):
- RegEst_form = forms.RegEstudianteForm()
- if request.method == 'POST':
- RegEst_form = RegEstudianteForm(data=request.POST)
- if RegEst_form.is_valid():
- RegEst_form.save()
- return render(request, 'basic_app/PagRegEstudiante.html', {'RegEst_form':RegEst_form})
- @login_required
- def special(request):
- # Remember to also set login url in settings.py!
- # LOGIN_URL = '/basic_app/user_login/'
- return HttpResponse("You are logged in")
- @login_required
- def user_logout(request):
- # Log out the user.
- logout(request)
- # Return to homepage.
- return HttpResponseRedirect(reverse('index'))
- def register(request):
- registered = False
- if request.method == 'POST':
- # Get info from "both" forms
- # It appears as one form to the user on the .html page
- user_form = UserForm(data=request.POST)
- profile_form = UserProfileInfoForm(data=request.POST)
- # Check to see both forms are valid
- if user_form.is_valid() and profile_form.is_valid():
- # Save User Form to Database
- user = user_form.save()
- user.set_password(user.password)
- user.save()
- profile = profile_form.save(commit=False)
- # Set One to One relationship between
- # UserForm and UserProfileInfoForm
- profile.user = user
- # Check if they provided a profile picture
- if 'profile_pic' in request.FILES:
- print('found picture')
- # If yes, then grab it from the POST form reply
- profile.profile_pic = request.FILES['profile_pic']
- # Now save model
- profile.save()
- # Registration Successful!
- registered = True
- else:
- # One of the forms was invalid if this else gets called.
- print(user_form.errors,profile_form.errors)
- else:
- # Was not an HTTP post so we just render the forms as blank.
- user_form = UserForm()
- profile_form = UserProfileInfoForm()
- # This is the render and context dictionary to feed
- # back to the registration.html file page.
- return render(request,'basic_app/registration.html',
- {'user_form':user_form,
- 'profile_form':profile_form,
- 'registered':registered})
- def user_login(request):
- if request.method == 'POST':
- # First get the username and password supplied
- username = request.POST.get('username')
- password = request.POST.get('password')
- # Django's built-in authentication function:
- user = authenticate(username=username, password=password)
- if user:
- if user.is_active:
- login(request,user)
- return HttpResponseRedirect(reverse('index'))
- else:
- return HttpResponse("Your account is not active.")
- else:
- print("Someone tried to login and failed.")
- print("They used username: {} and password: {}".format(username,password))
- return HttpResponse("Invalid login details supplied.")
- else:
- #Nothing has been provided for username or password.
- return render(request, 'basic_app/login.html', {})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement