Bläddra i källkod

token auth (partial)

test 3 år sedan
förälder
incheckning
709fcc26c3
3 ändrade filer med 50 tillägg och 7 borttagningar
  1. 26 2
      webgui/config/models.py
  2. 14 4
      webgui/config/views.py
  3. 10 1
      webgui/stats/models.py

+ 26 - 2
webgui/config/models.py

@@ -16,8 +16,32 @@ class ChannelSettings(models.Model):
 		default = False,
 		help_text = "Allow the robot to speak and interact with users."
 	)
-	
+
 	class Meta:
 		managed = False
 		db_table = "channel_settings"
-		#verbose_name_plural = "accounts"
+		#verbose_name_plural = "accounts"
+
+class GuildAccessToken(models.Model):
+	id = models.AutoField(
+		primary_key = True,
+	)
+	guild = models.ForeignKey(
+		"stats.Guild",
+		on_delete = models.CASCADE,
+		to_field = "guild_id",
+		db_column = "guild",
+	)
+	user = models.ForeignKey(
+		"stats.User",
+		on_delete = models.Cascade,
+		to_field = "user_id",
+		db_column = "user",
+	)
+	token = models.Charfield(
+		max_length = 40,
+		#not_null = True,
+		
+	)
+	token varchar[40] UNIQUE NOT NULL, \
+	created TIMESTAMP NOT NULL DEFAULT now()\

+ 14 - 4
webgui/config/views.py

@@ -1,10 +1,20 @@
 from django.shortcuts import render, get_object_or_404
 from webgui.settings import APPLICATION_NAME
-from .models import ChannelSettings
+from .models import ChannelSettings, GuildAccessToken
 from .forms import ChannelSettingsForm
 
-def channel_settings(request, channel_id):
+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':
@@ -14,7 +24,7 @@ def channel_settings(request, channel_id):
 			updated = True
 	else:
 		form = ChannelSettingsForm(instance=settings)
-	
+
 	context = {
 		'title': 'Channel settings',
 		'icon': 'screwdriver',
@@ -23,4 +33,4 @@ def channel_settings(request, channel_id):
 		'form': form,
 		'updated': updated,
 	}
-	return render(request, 'config/channel_settings.html', context)
+	return render(request, 'config/channel_settings.html', context)

+ 10 - 1
webgui/stats/models.py

@@ -34,4 +34,13 @@ class Channel(models.Model):
 	class Meta:
 		managed = False
 		db_table = "channel"
-		verbose_name_plural = "channels"
+		verbose_name_plural = "channels"
+
+class User(models.model):
+	id = models.Autofield(
+		primary_key = True,
+	)
+	user_id = models.BigIntegerField(
+		unique = True,
+		#not_null = True,
+	)