views.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from django.shortcuts import render, get_object_or_404
  2. from webgui.settings import APPLICATION_NAME
  3. from stats.models import Channel
  4. from .models import GuildAccessToken
  5. from .forms import ChannelSettingsForm
  6. import datetime
  7. def channel_settings(request, channel_id, token):
  8. # Check if channel and token exists.
  9. channel = get_object_or_404(Channel, channel_id=channel_id)
  10. #settings = get_object(ChannelSettings, channel=channel_id)
  11. token = get_object_or_404(GuildAccessToken, token="{"+token+"}")
  12. # Check if token is valid.
  13. #print(token.created.replace(tzinfo=None))
  14. #print(token.created.replace(tzinfo=None) + datetime.timedelta(hours=-1))
  15. #print(datetime.datetime.now() - datetime.timedelta(minutes=10))
  16. token_expired = True
  17. if token.created.replace(tzinfo=None) + datetime.timedelta(hours=-1) > datetime.datetime.now() - datetime.timedelta(minutes=10):
  18. token_expired = False
  19. token_invalid = True
  20. if token.guild == channel.guild:
  21. token_invalid = False
  22. updated = False
  23. if request.method == 'POST' and not token_expired and not token_invalid:
  24. form = ChannelSettingsForm(request.POST, instance=channel)
  25. if form.is_valid():
  26. form.save()
  27. updated = True
  28. else:
  29. form = ChannelSettingsForm(instance=channel)
  30. context = {
  31. 'title': 'Channel settings',
  32. 'icon': 'screwdriver',
  33. 'description': 'Modify channel settings for ' + APPLICATION_NAME,
  34. 'keywords': 'settings, channel',
  35. 'form': form,
  36. 'updated': updated,
  37. 'token_expired': token_expired,
  38. 'token_invalid': token_invalid,
  39. }
  40. return render(request, 'config/channel_settings.html', context)