views.py 6.1 KB

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