from django.shortcuts import render, get_object_or_404
from webgui.settings import APPLICATION_NAME
from .models import ChannelSettings, GuildAccessToken
from .forms import ChannelSettingsForm

def channel_settings(request, channel_id, slug):
	# Check if channel and token exists.
	settings = get_object_or_404(ChannelSettings, channel=channel_id)
	token = get_object_or_404(GuildAccessToken, slug=token)

	# Check if token is valid.
    print(key.created.replace(tzinfo=None))
    print(datetime.datetime.now() - datetime.timedelta(minutes=10))
    if key.created.replace(tzinfo=None) + datetime.timedelta(hours=1)> datetime.datetime.now() - datetime.timedelta(minutes=10):
        key_expired = False
    else:
        key_expired = True

	updated = False
	if request.method == 'POST':
		form = ChannelSettingsForm(request.POST, instance=settings)
		if form.is_valid():
			form.save()
			updated = True
	else:
		form = ChannelSettingsForm(instance=settings)

	context = {
		'title': 'Channel settings',
		'icon': 'screwdriver',
		'description': 'Modify channel settings for ' + APPLICATION_NAME,
		'keywords': 'settigns, channel',
		'form': form,
		'updated': updated,
	}
	return render(request, 'config/channel_settings.html', context)
