views.py 6.2 KB

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