Advertisement
Guest User

Untitled

a guest
Nov 9th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. from basic_app.forms import UserProfileInfoForm
  2. from basic_app.forms import PrestamoForm
  3. from basic_app.forms import RegEstudianteForm
  4.  
  5.  
  6. # Extra Imports for the Login and Logout Capabilities
  7. from django.contrib.auth import authenticate, login, logout
  8. from django.http import HttpResponseRedirect, HttpResponse
  9. from django.core.urlresolvers import reverse
  10. from django.contrib.auth.decorators import login_required
  11. from django.shortcuts import render_to_response
  12.  
  13.  
  14. def index(request):
  15. return render(request,'basic_app/index.html')
  16.  
  17. #def webIApage(request):
  18. # return render(request, 'basic_app/camera.html')
  19.  
  20. @login_required
  21. def prestamo(request):
  22. Prest_form = forms.PrestamoForm()
  23. if request.method == 'POST':
  24. Prest_form = PrestamoForm(data=request.POST)
  25. if Prest_form.is_valid():
  26. Prest_form.save()
  27.  
  28. return render(request, 'basic_app/PagPrestamo.html', {'Prest_form':Prest_form})
  29.  
  30. @login_required
  31. def registroEstudiantes(request):
  32. RegEst_form = forms.RegEstudianteForm()
  33. if request.method == 'POST':
  34. RegEst_form = RegEstudianteForm(data=request.POST)
  35. if RegEst_form.is_valid():
  36. RegEst_form.save()
  37.  
  38. return render(request, 'basic_app/PagRegEstudiante.html', {'RegEst_form':RegEst_form})
  39.  
  40.  
  41. @login_required
  42. def special(request):
  43. # Remember to also set login url in settings.py!
  44. # LOGIN_URL = '/basic_app/user_login/'
  45. return HttpResponse("You are logged in")
  46.  
  47. @login_required
  48. def user_logout(request):
  49. # Log out the user.
  50. logout(request)
  51. # Return to homepage.
  52. return HttpResponseRedirect(reverse('index'))
  53.  
  54. def register(request):
  55.  
  56. registered = False
  57.  
  58. if request.method == 'POST':
  59.  
  60. # Get info from "both" forms
  61. # It appears as one form to the user on the .html page
  62. user_form = UserForm(data=request.POST)
  63. profile_form = UserProfileInfoForm(data=request.POST)
  64.  
  65. # Check to see both forms are valid
  66. if user_form.is_valid() and profile_form.is_valid():
  67.  
  68. # Save User Form to Database
  69. user = user_form.save()
  70. user.set_password(user.password)
  71. user.save()
  72.  
  73. profile = profile_form.save(commit=False)
  74. # Set One to One relationship between
  75. # UserForm and UserProfileInfoForm
  76. profile.user = user
  77. # Check if they provided a profile picture
  78. if 'profile_pic' in request.FILES:
  79. print('found picture')
  80. # If yes, then grab it from the POST form reply
  81. profile.profile_pic = request.FILES['profile_pic']
  82. # Now save model
  83. profile.save()
  84. # Registration Successful!
  85. registered = True
  86.  
  87. else:
  88. # One of the forms was invalid if this else gets called.
  89. print(user_form.errors,profile_form.errors)
  90.  
  91. else:
  92. # Was not an HTTP post so we just render the forms as blank.
  93. user_form = UserForm()
  94. profile_form = UserProfileInfoForm()
  95.  
  96. # This is the render and context dictionary to feed
  97. # back to the registration.html file page.
  98. return render(request,'basic_app/registration.html',
  99. {'user_form':user_form,
  100. 'profile_form':profile_form,
  101. 'registered':registered})
  102.  
  103. def user_login(request):
  104.  
  105. if request.method == 'POST':
  106. # First get the username and password supplied
  107. username = request.POST.get('username')
  108. password = request.POST.get('password')
  109.  
  110. # Django's built-in authentication function:
  111. user = authenticate(username=username, password=password)
  112.  
  113. if user:
  114. if user.is_active:
  115. login(request,user)
  116. return HttpResponseRedirect(reverse('index'))
  117. else:
  118. return HttpResponse("Your account is not active.")
  119. else:
  120. print("Someone tried to login and failed.")
  121. print("They used username: {} and password: {}".format(username,password))
  122. return HttpResponse("Invalid login details supplied.")
  123.  
  124. else:
  125. #Nothing has been provided for username or password.
  126. return render(request, 'basic_app/login.html', {})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement