Finished API tokens

reminder
Etzelia 2018-11-21 21:15:21 -06:00
parent 7ae8fa576d
commit 20c97188ef
5 changed files with 37 additions and 27 deletions

View File

@ -5,6 +5,7 @@ from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from minecraft_manager.models import Application, Warning, Ticket, Player, IP, UserSettings, Alert, Note 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): class PlayerInline(admin.StackedInline):
@ -100,6 +101,7 @@ try:
admin.site.register(IP, IPAdmin) admin.site.register(IP, IPAdmin)
admin.site.register(Alert) admin.site.register(Alert)
admin.site.register(Note) admin.site.register(Note)
api_register()
except admin.sites.AlreadyRegistered: except admin.sites.AlreadyRegistered:
pass pass

View File

@ -22,6 +22,9 @@ class TokenActiveFilter(admin.SimpleListFilter):
class TokenAdmin(admin.ModelAdmin): class TokenAdmin(admin.ModelAdmin):
list_filter = (TokenActiveFilter,) 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 = ( fieldsets = (
(None, { (None, {
'fields': ('key', 'active') 'fields': ('key', 'active')
@ -34,8 +37,9 @@ class TokenAdmin(admin.ModelAdmin):
) )
try: def register():
admin.site.register(Token, TokenAdmin) try:
except admin.sites.AlreadyRegistered: admin.site.register(Token, TokenAdmin)
pass except admin.sites.AlreadyRegistered:
pass

View File

@ -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 minecraft_manager.models import Alert
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.conf import settings from django.conf import settings
@ -140,4 +140,8 @@ def get_query():
'players': sorted(query.players.names)} 'players': sorted(query.players.names)}
except: except:
return {'max': 0, 'online': 0, return {'max': 0, 'online': 0,
'players': []} 'players': []}
def generate_password(size=20):
return "".join([random.choice(string.ascii_letters + string.digits) for idx in range(0, size)])

View File

@ -1,16 +1,20 @@
from django.db import models from django.db import models
from minecraft_manager.api.api import generate_password
class Token(models.Model): 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) active = models.BooleanField("Active", default=True)
web_get_permission = models.BooleanField("Web API GET", default=False) web_get_permission = models.BooleanField("Web GET", default=False)
web_post_permission = models.BooleanField("Web API POST", default=False) web_post_permission = models.BooleanField("Web POST", default=False)
plugin_get_permission = models.BooleanField("Plugin API GET", default=False) plugin_get_permission = models.BooleanField("Plugin GET", default=False)
plugin_post_permission = models.BooleanField("Plugin API POST", default=False) plugin_post_permission = models.BooleanField("Plugin POST", default=False)
form_get_permission = models.BooleanField("Form API GET", default=False) form_get_permission = models.BooleanField("Form GET", default=False)
form_post_permission = models.BooleanField("Form API POST", default=False) form_post_permission = models.BooleanField("Form POST", default=False)
model_get_permission = models.BooleanField("Model API GET", default=False) model_get_permission = models.BooleanField("Model GET", default=False)
model_post_permission = models.BooleanField("Model API POST", default=False) model_post_permission = models.BooleanField("Model POST", default=False)
stats_get_permission = models.BooleanField("Stats API GET", default=False) stats_get_permission = models.BooleanField("Stats GET", default=False)
stats_post_permission = models.BooleanField("Stats API POST", default=False) stats_post_permission = models.BooleanField("Stats POST", default=False)
def __str__(self):
return self.key

View File

@ -1,6 +1,6 @@
from __future__ import absolute_import from __future__ import absolute_import
import logging, random, string, datetime import logging, datetime
from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth import update_session_auth_hash from django.contrib.auth import update_session_auth_hash
from django.apps import apps from django.apps import apps
@ -11,7 +11,7 @@ from django.utils import timezone
from django.views.generic import View from django.views.generic import View
from django.forms import modelform_factory 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 from minecraft_manager.models import Player, UserSettings, Application, IP, Ticket, Warning
import minecraft_manager.api.api as mcm_api import minecraft_manager.api.api as mcm_api
from minecraft_manager.api.models import Token from minecraft_manager.api.models import Token
@ -51,10 +51,6 @@ def clean(model, data):
return cleaned return cleaned
def generate_password():
return "".join([random.choice(string.ascii_letters + string.digits) for idx in range(0, 20)])
class WebAPI(View): class WebAPI(View):
def get(self, request, keyword): def get(self, request, keyword):
@ -119,7 +115,7 @@ class WebAPI(View):
else: else:
return HttpResponse(form.as_p()) return HttpResponse(form.as_p())
elif keyword == 'alert': elif keyword == 'alert':
form = MCMForms.AlertForm(request.POST) form = mcm_forms.AlertForm(request.POST)
if form.is_valid(): if form.is_valid():
if mcm_api.create_alert(form.cleaned_data['message']): if mcm_api.create_alert(form.cleaned_data['message']):
data = {'success': True} data = {'success': True}
@ -284,7 +280,7 @@ class PluginAPI(View):
json['status'] = False json['status'] = False
json['message'] = "You are already registered. To change your password, contact an Admin." json['message'] = "You are already registered. To change your password, contact an Admin."
else: else:
password = generate_password() password = mcm_api.generate_password()
user = User.objects.create_user(username=player.username.lower(), password=password) user = User.objects.create_user(username=player.username.lower(), password=password)
user.save() user.save()
player.auth_user = user player.auth_user = user
@ -332,7 +328,7 @@ class FormAPI(View):
break break
if model: if model:
form = None form = None
for modelform in MCMForms.__all__(): for modelform in mcm_forms.__all__():
if modelform.Meta.model == model: if modelform.Meta.model == model:
form = modelform() form = modelform()
break break
@ -355,7 +351,7 @@ class FormAPI(View):
break break
if model: if model:
form = None form = None
for modelform in MCMForms.__all__(): for modelform in mcm_forms.__all__():
if modelform.Meta.model == model: if modelform.Meta.model == model:
form = modelform(post) form = modelform(post)
break break