From 20c97188ef20ef67a4dff36dbf4e135a3aa6734c Mon Sep 17 00:00:00 2001 From: Etzelia Date: Wed, 21 Nov 2018 21:15:21 -0600 Subject: [PATCH] Finished API tokens --- admin.py | 2 ++ api/admin.py | 12 ++++++++---- api/api.py | 8 ++++++-- api/models.py | 26 +++++++++++++++----------- api/views.py | 16 ++++++---------- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/admin.py b/admin.py index a06e53e..e01ee22 100644 --- a/admin.py +++ b/admin.py @@ -5,6 +5,7 @@ from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ from minecraft_manager.models import Application, Warning, Ticket, Player, IP, UserSettings, Alert, Note +from minecraft_manager.api.admin import register as api_register class PlayerInline(admin.StackedInline): @@ -100,6 +101,7 @@ try: admin.site.register(IP, IPAdmin) admin.site.register(Alert) admin.site.register(Note) + api_register() except admin.sites.AlreadyRegistered: pass diff --git a/api/admin.py b/api/admin.py index 51fced1..99044c7 100644 --- a/api/admin.py +++ b/api/admin.py @@ -22,6 +22,9 @@ class TokenActiveFilter(admin.SimpleListFilter): class TokenAdmin(admin.ModelAdmin): list_filter = (TokenActiveFilter,) + list_display = ('key', 'active', 'web_get_permission', 'web_post_permission', 'plugin_get_permission', + 'plugin_post_permission', 'form_get_permission', 'form_post_permission', 'model_get_permission', + 'model_post_permission', 'stats_get_permission', 'stats_post_permission') fieldsets = ( (None, { 'fields': ('key', 'active') @@ -34,8 +37,9 @@ class TokenAdmin(admin.ModelAdmin): ) -try: - admin.site.register(Token, TokenAdmin) -except admin.sites.AlreadyRegistered: - pass +def register(): + try: + admin.site.register(Token, TokenAdmin) + except admin.sites.AlreadyRegistered: + pass diff --git a/api/api.py b/api/api.py index 411235f..eadc7dc 100644 --- a/api/api.py +++ b/api/api.py @@ -1,4 +1,4 @@ -import socket, requests, logging, os, datetime, pytz, mcstatus, discord +import socket, requests, logging, os, datetime, pytz, mcstatus, random, string from minecraft_manager.models import Alert from django.contrib.auth.models import User from django.conf import settings @@ -140,4 +140,8 @@ def get_query(): 'players': sorted(query.players.names)} except: return {'max': 0, 'online': 0, - 'players': []} \ No newline at end of file + 'players': []} + + +def generate_password(size=20): + return "".join([random.choice(string.ascii_letters + string.digits) for idx in range(0, size)]) \ No newline at end of file diff --git a/api/models.py b/api/models.py index 6dbdb29..e70bddf 100644 --- a/api/models.py +++ b/api/models.py @@ -1,16 +1,20 @@ from django.db import models +from minecraft_manager.api.api import generate_password class Token(models.Model): - key = models.CharField("Key", max_length=50, unique=True) + key = models.CharField("Key", default=generate_password, max_length=50, unique=True) active = models.BooleanField("Active", default=True) - web_get_permission = models.BooleanField("Web API GET", default=False) - web_post_permission = models.BooleanField("Web API POST", default=False) - plugin_get_permission = models.BooleanField("Plugin API GET", default=False) - plugin_post_permission = models.BooleanField("Plugin API POST", default=False) - form_get_permission = models.BooleanField("Form API GET", default=False) - form_post_permission = models.BooleanField("Form API POST", default=False) - model_get_permission = models.BooleanField("Model API GET", default=False) - model_post_permission = models.BooleanField("Model API POST", default=False) - stats_get_permission = models.BooleanField("Stats API GET", default=False) - stats_post_permission = models.BooleanField("Stats API POST", default=False) + web_get_permission = models.BooleanField("Web GET", default=False) + web_post_permission = models.BooleanField("Web POST", default=False) + plugin_get_permission = models.BooleanField("Plugin GET", default=False) + plugin_post_permission = models.BooleanField("Plugin POST", default=False) + form_get_permission = models.BooleanField("Form GET", default=False) + form_post_permission = models.BooleanField("Form POST", default=False) + model_get_permission = models.BooleanField("Model GET", default=False) + model_post_permission = models.BooleanField("Model POST", default=False) + stats_get_permission = models.BooleanField("Stats GET", default=False) + stats_post_permission = models.BooleanField("Stats POST", default=False) + + def __str__(self): + return self.key diff --git a/api/views.py b/api/views.py index 603da56..26094ba 100644 --- a/api/views.py +++ b/api/views.py @@ -1,6 +1,6 @@ from __future__ import absolute_import -import logging, random, string, datetime +import logging, datetime from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth import update_session_auth_hash from django.apps import apps @@ -11,7 +11,7 @@ from django.utils import timezone from django.views.generic import View from django.forms import modelform_factory -import minecraft_manager.forms as MCMForms +import minecraft_manager.forms as mcm_forms from minecraft_manager.models import Player, UserSettings, Application, IP, Ticket, Warning import minecraft_manager.api.api as mcm_api from minecraft_manager.api.models import Token @@ -51,10 +51,6 @@ def clean(model, data): return cleaned -def generate_password(): - return "".join([random.choice(string.ascii_letters + string.digits) for idx in range(0, 20)]) - - class WebAPI(View): def get(self, request, keyword): @@ -119,7 +115,7 @@ class WebAPI(View): else: return HttpResponse(form.as_p()) elif keyword == 'alert': - form = MCMForms.AlertForm(request.POST) + form = mcm_forms.AlertForm(request.POST) if form.is_valid(): if mcm_api.create_alert(form.cleaned_data['message']): data = {'success': True} @@ -284,7 +280,7 @@ class PluginAPI(View): json['status'] = False json['message'] = "You are already registered. To change your password, contact an Admin." else: - password = generate_password() + password = mcm_api.generate_password() user = User.objects.create_user(username=player.username.lower(), password=password) user.save() player.auth_user = user @@ -332,7 +328,7 @@ class FormAPI(View): break if model: form = None - for modelform in MCMForms.__all__(): + for modelform in mcm_forms.__all__(): if modelform.Meta.model == model: form = modelform() break @@ -355,7 +351,7 @@ class FormAPI(View): break if model: form = None - for modelform in MCMForms.__all__(): + for modelform in mcm_forms.__all__(): if modelform.Meta.model == model: form = modelform(post) break