spradio-server-django/savepointradio/core/utils.py

92 lines
2.8 KiB
Python
Raw Normal View History

2017-12-27 21:11:20 +00:00
import random
2018-01-05 20:18:12 +00:00
import re
2017-12-27 21:11:20 +00:00
import string
2018-01-05 20:18:12 +00:00
from unicodedata import normalize
2017-12-27 21:11:20 +00:00
from django.core.exceptions import ObjectDoesNotExist
from django.db import connection
from .models import Setting
def generate_password(length=32):
2017-12-28 19:44:01 +00:00
chars = string.ascii_letters + string.digits + string.punctuation
2017-12-27 21:11:20 +00:00
rng = random.SystemRandom()
2017-12-28 19:44:01 +00:00
return ''.join([rng.choice(chars) for i in range(length)])
2017-12-27 21:11:20 +00:00
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]
2017-12-28 19:44:01 +00:00
sql = ''.join(('SELECT COUNT(*) FROM (',
rawqueryset.raw_query.format(tuple(params)),
') B;'))
2017-12-27 21:11:20 +00:00
cursor = connection.cursor()
cursor.execute(sql)
row = cursor.fetchone()
return row[0]
return __len__
2017-12-28 19:44:01 +00:00
2017-12-27 21:11:20 +00:00
def get_setting(name):
setting = Setting.objects.get(name=name)
return setting.get()
2017-12-28 19:44:01 +00:00
2017-12-27 21:11:20 +00:00
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:
2017-12-28 19:44:01 +00:00
error_msg = 'New settings need type (Integer, Float, String, Bool)'
2017-12-27 21:11:20 +00:00
raise TypeError(error_msg)
return
2018-01-05 20:18:12 +00:00
def naturalize(text):
2018-01-05 20:18:12 +00:00
"""
Return a normalized unicode string, with removed starting articles, for use
in natural sorting.
Code was inspired by 'django-naturalsortfield' from Nathan Reynolds:
https://github.com/nathforge/django-naturalsortfield
"""
def naturalize_int_match(match):
2018-01-06 18:11:51 +00:00
return '{:08d}'.format(int(match.group(0)))
2018-01-05 20:18:12 +00:00
text = normalize('NFKD', text).encode('ascii', 'ignore').decode('ascii')
text = text.lower()
punc = re.compile('[{}]'.format(re.escape(string.punctuation)))
text = re.sub(punc, ' ', text)
text = text.strip()
text = re.sub(r'^(a|an|the)\s+', '', text)
text = re.sub(r'\d+', naturalize_int_match, text)
2018-01-05 20:18:12 +00:00
return text
2018-01-14 20:21:04 +00:00
def build_message_start(quantity, model):
"""
The beggining of a message based on the quantity and singular/plural name
of the model involved.
"""
if quantity == 1:
message = '1 {} was'.format(model._meta.verbose_name)
else:
message = '{} {} were'.format(str(quantity),
model._meta.verbose_name_plural)
return message