Add api app with ApiToken model

This commit is contained in:
Paweł Jastrzębski 2021-10-11 11:33:18 +02:00 committed by R. Miles McCain
parent 1fd46b019c
commit ff6933b4de
9 changed files with 65 additions and 0 deletions

0
shynet/api/__init__.py Normal file
View File

3
shynet/api/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
shynet/api/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api'

View File

@ -0,0 +1,30 @@
# Generated by Django 3.2.5 on 2021-10-11 09:31
import api.models
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='ApiToken',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=64)),
('value', models.TextField(default=api.models._default_token_value, unique=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='api_tokens', to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ['name', 'value'],
},
),
]

View File

19
shynet/api/models.py Normal file
View File

@ -0,0 +1,19 @@
from django.db import models
from core.models import User
from secrets import token_urlsafe
def _default_token_value():
return token_urlsafe(32)
class ApiToken(models.Model):
name = models.CharField(max_length=64)
value = models.TextField(default=_default_token_value, unique=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="api_tokens")
class Meta:
ordering = ["name", "value"]
def __str__(self):
return self.name

3
shynet/api/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
shynet/api/views.py Normal file
View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

@ -59,6 +59,7 @@ INSTALLED_APPS = [
"core", "core",
"dashboard.apps.DashboardConfig", "dashboard.apps.DashboardConfig",
"analytics", "analytics",
"api",
"allauth", "allauth",
"allauth.account", "allauth.account",
"allauth.socialaccount", "allauth.socialaccount",