Initial Core app commit.

This commit is contained in:
Josh Washburne 2017-12-27 16:11:20 -05:00
parent 739820d401
commit fa0677de95
10 changed files with 179 additions and 0 deletions

View file

View file

@ -0,0 +1,10 @@
from django.contrib import admin
from authtools.admin import UserAdmin
from .models import RadioUser
@admin.register(RadioUser)
class RadioUserAdmin(UserAdmin):
pass

View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class CoreConfig(AppConfig):
name = 'core'

View file

@ -0,0 +1,11 @@
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
class Timestampable(models.Model):
created_date = models.DateTimeField(_('added on'), auto_now_add=True)
modified_date = models.DateTimeField(_('last modified'), auto_now=True)
class Meta:
abstract = True

View file

@ -0,0 +1,47 @@
# Generated by Django 2.0 on 2017-12-27 21:02
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0009_alter_user_last_name_max_length'),
]
operations = [
migrations.CreateModel(
name='RadioUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('email', models.EmailField(max_length=255, unique=True, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('name', models.CharField(max_length=255, verbose_name='name')),
('is_dj', models.BooleanField(default=False, help_text='Designates whether this user is the automated dj account or is a real person account.', verbose_name='dj status')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'ordering': ['name', 'email'],
'abstract': False,
},
),
migrations.CreateModel(
name='Setting',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=64, unique=True)),
('description', models.TextField(blank=True)),
('setting_type', models.PositiveIntegerField(choices=[(0, 'Integer'), (1, 'Float'), (2, 'String'), (3, 'Bool')], default=0)),
('data', models.TextField()),
],
),
]

View file

@ -0,0 +1,51 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from authtools.models import AbstractNamedUser
class RadioUser(AbstractNamedUser):
"""
Custom user model which uses email as the main identifier and adds a flag
for whether the user is the radio DJ.
"""
is_dj = models.BooleanField(_('dj status'),
default=False,
help_text=_('Designates whether this user is '
'the automated dj account or is a '
'real person account.'))
class Setting(models.Model):
"""
A model for keeping track of dynamic settings while the site is online and
the radio is running.
"""
INTEGER = 0
FLOAT = 1
STRING = 2
BOOL = 3
TYPE_CHOICES = (
(INTEGER, 'Integer'),
(FLOAT, 'Float'),
(STRING, 'String'),
(BOOL, 'Bool'),
)
name = models.CharField(max_length=64, unique=True)
description = models.TextField(blank=True)
setting_type = models.PositiveIntegerField(choices=TYPE_CHOICES,
default=INTEGER)
data = models.TextField()
def get(self):
if self.setting_type == self.INTEGER:
return int(self.data)
elif self.setting_type == self.FLOAT:
return float(self.data)
elif self.setting_type == self.BOOL:
return self.data == 'True'
else:
return self.data
def __str__(self):
return '{}: {}'.format(self.name, self.data)

View file

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

View file

@ -0,0 +1,49 @@
import random
import string
from django.core.exceptions import ObjectDoesNotExist
from django.db import connection
from .models import Setting
def generate_password(length=32):
possible_characters = string.ascii_letters + string.digits + string.punctuation
rng = random.SystemRandom()
return ''.join([rng.choice(possible_characters) for i in range(length)])
def get_len(rawqueryset):
"""
Adds/Overrides a dynamic implementation of the length protocol to the
definition of RawQuerySet.
"""
def __len__(self):
params = ['{}'.format(p) for p in self.params]
sql = 'SELECT COUNT(*) FROM (' + rawqueryset.raw_query.format(tuple(params)) + ') B;'
cursor = connection.cursor()
cursor.execute(sql)
row = cursor.fetchone()
return row[0]
return __len__
def get_setting(name):
setting = Setting.objects.get(name=name)
return setting.get()
def set_setting(name, value, setting_type=None):
setting_types = {'Integer': 0, 'Float': 1, 'String': 2, 'Bool': 3}
try:
setting = Setting.objects.get(name=name)
setting.data = str(value)
if setting_type in setting_types:
setting.setting_type = setting_types[setting_type]
setting.save()
except ObjectDoesNotExist:
if setting_type in setting_types:
Setting.objects.create(name=name,
setting_type=setting_types[setting_type],
data=str(value))
else:
error_msg = 'New settings need a type (Integer, Float, String, Bool)'
raise TypeError(error_msg)
return

View file

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