views.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from django.shortcuts import render
  2. from hackathon.forms import UserForm
  3. from django.contrib.auth import logout
  4. from django.template import RequestContext, loader
  5. from django.contrib.auth import authenticate, login
  6. from django.http import HttpResponse, HttpResponseRedirect
  7. from scripts.steam import gamesPulling, steamIDPulling
  8. from scripts.github import getUserData
  9. def index(request):
  10. context = {'hello': 'world'}
  11. return render(request, 'hackathon/index.html', context)
  12. def test(request):
  13. return HttpResponse('meow')
  14. def api_examples(request):
  15. context = {'title': 'API Examples Page'}
  16. return render(request, 'hackathon/api_examples.html', context)
  17. def register(request):
  18. # A boolean value for telling the template whether the registration was successful.
  19. # Set to False initially. Code changes value to True when registration succeeds.
  20. registered = False
  21. # If it's a HTTP POST, we're interested in processing form data.
  22. if request.method == 'POST':
  23. # Attempt to grab information from the raw form information.
  24. # Note that we make use of both UserForm and UserProfileForm.
  25. user_form = UserForm(data=request.POST)
  26. # If the two forms are valid...
  27. if user_form.is_valid():
  28. # Save the user's form data to the database.
  29. user = user_form.save()
  30. # Now we hash the password with the set_password method.
  31. # Once hashed, we can update the user object.
  32. user.set_password(user.password)
  33. user.save()
  34. # Update our variable to tell the template registration was successful.
  35. registered = True
  36. # Invalid form or forms - mistakes or something else?
  37. # Print problems to the terminal.
  38. # They'll also be shown to the user.
  39. else:
  40. print user_form.errors
  41. # Not a HTTP POST, so we render our form using two ModelForm instances.
  42. # These forms will be blank, ready for user input.
  43. else:
  44. user_form = UserForm()
  45. # Render the template depending on the context.
  46. return render(request,
  47. 'hackathon/register.html',
  48. {'user_form': user_form, 'registered': registered} )
  49. def user_login(request):
  50. # If the request is a HTTP POST, try to pull out the relevant information.
  51. if request.method == 'POST':
  52. # Gather the username and password provided by the user.
  53. # This information is obtained from the login form.
  54. # We use request.POST.get('<variable>') as opposed to request.POST['<variable>'],
  55. # because the request.POST.get('<variable>') returns None, if the value does not exist,
  56. # while the request.POST['<variable>'] will raise key error exception
  57. username = request.POST.get('username')
  58. password = request.POST.get('password')
  59. # Use Django's machinery to attempt to see if the username/password
  60. # combination is valid - a User object is returned if it is.
  61. user = authenticate(username=username, password=password)
  62. # If we have a User object, the details are correct.
  63. # If None (Python's way of representing the absence of a value), no user
  64. # with matching credentials was found.
  65. if user:
  66. # Is the account active? It could have been disabled.
  67. if user.is_active:
  68. # If the account is valid and active, we can log the user in.
  69. # We'll send the user back to the homepage.
  70. login(request, user)
  71. return HttpResponseRedirect('/hackathon/')
  72. else:
  73. # An inactive account was used - no logging in!
  74. return HttpResponse("Your Django Hackathon account is disabled.")
  75. else:
  76. # Bad login details were provided. So we can't log the user in.
  77. print "Invalid login details: {0}, {1}".format(username, password)
  78. return HttpResponse("Invalid login details supplied.")
  79. # The request is not a HTTP POST, so display the login form.
  80. # This scenario would most likely be a HTTP GET.
  81. else:
  82. # No context variables to pass to the template system, hence the
  83. # blank dictionary object...
  84. return render(request, 'hackathon/login.html', {})
  85. def user_logout(request):
  86. # Since we know the user is logged in, we can now just log them out.
  87. logout(request)
  88. # Take the user back to the homepage.
  89. return HttpResponseRedirect('/hackathon/')
  90. def steam(request):
  91. #Should link to test of Steam API example.
  92. key = '231E98D442E52B87110816C3D5114A1D'
  93. SteamUN = "Marorin"
  94. steamID = steamIDPulling(SteamUN, key)
  95. game = gamesPulling(steamID, key)
  96. return render(request,'hackathon/steam.html', {"game": game })
  97. def github(request):
  98. userData = getUserData()
  99. return render(request, 'hackathon/github.html', { 'data': userData })
  100. def tumblr(request):
  101. context = {'title': 'Tumblr Example'}
  102. return render(request, 'hackathon/tumblr.html', context)