views.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from django.shortcuts import render, redirect
  2. from django.views import generic
  3. from django.views.generic.edit import CreateView, UpdateView, DeleteView
  4. from django.http import HttpResponseRedirect
  5. from django.urls import reverse, reverse_lazy
  6. from django.contrib.auth.models import User
  7. from django.contrib.auth import authenticate, login
  8. from django.contrib.auth.mixins import LoginRequiredMixin
  9. from django.contrib.auth.decorators import login_required
  10. from .models import Canvas, IdeaCategory, CanvasTag, Idea, Comment
  11. from .forms import SignUpForm
  12. # TODO: update views based on models
  13. # TODO: use django.utils.timezone instead of datetime
  14. import django.utils.timezone
  15. @login_required
  16. def new_canvas(request):
  17. canvas = Canvas(title = "New Canvas", public = False)
  18. canvas.save()
  19. canvas.admins.add(request.user)
  20. canvas.users.add(request.user)
  21. canvas.save()
  22. return redirect(canvas.get_absolute_url()) # bring user to the canvas page for the newly created canvas
  23. def index(request):
  24. return render(request, 'index.html')
  25. def register(request):
  26. if request.method == 'POST':
  27. form = SignUpForm(request.POST)
  28. if form.is_valid():
  29. username = form.cleaned_data['name']
  30. email = form.cleaned_data['email']
  31. password = form.cleaned_data['password']
  32. newUser = User.objects.create_user(username = username, email = email, password = password)
  33. return HttpResponseRedirect(reverse('index'))
  34. else:
  35. form = SignUpForm(initial = {
  36. 'name': '',
  37. 'email': '',
  38. 'password': '',
  39. 'password2': '',
  40. })
  41. return render(
  42. request,
  43. 'catalog/register.html',
  44. {'form': form}
  45. )
  46. class CanvasList(LoginRequiredMixin, generic.ListView):
  47. # TODO: update this based on changes in Model
  48. model = Canvas
  49. def get_context_data(self, **kwargs):
  50. '''
  51. This function's purpose is to separate the public from the private canvasses
  52. '''
  53. me = self.request.user
  54. context = super().get_context_data(**kwargs)
  55. filter_kwargs = {'public': True}
  56. # me_filter_kwargs = {'users': me}
  57. canvasses = Canvas.objects.all()
  58. # public canvasses are those where public is true
  59. public = canvasses.filter(**filter_kwargs)
  60. # private canvasses where the user is either the owner or a collaborator on the canvas
  61. private = canvasses.exclude(**filter_kwargs).filter(users__pk = me)
  62. context['public_canvas_list'] = public
  63. context['private_canvas_list'] = private
  64. return context
  65. class CanvasDetailView(LoginRequiredMixin, generic.DetailView):
  66. model = Canvas
  67. def get_context_data(self, **kwargs):
  68. '''
  69. This function is to return a set of ideas where their canvasID attribute matches the current canvas id.
  70. '''
  71. context = super().get_context_data(**kwargs)
  72. contextCID = (context['canvas'].pk)
  73. filter_kwargs = {'canvas': contextCID}
  74. ideas = Idea.objects.all()
  75. filtered_ideas = ideas.filter(**filter_kwargs)
  76. context['idea_list'] = filtered_ideas
  77. return context