Include numberedmodel and simplesass apps

These apps are so minimal they don't warrant their own repository. I'm
hoping they will find a new home here. This move kind of obsoletes the
github.com/rtts/django-numberedmodel and github.com/rtts/django-simplesass
repositories, although I'm not planning to update their documentation unless
I make any major changes here.
readwriteweb
Jaap Joris Vens 2019-11-22 11:57:10 +01:00
rodzic d2d4915de7
commit 88e420f4c0
9 zmienionych plików z 85 dodań i 45 usunięć

Wyświetl plik

@ -3,59 +3,16 @@ from django.urls import reverse
from django.conf import settings
from django.forms import TextInput
from django.utils.translation import gettext_lazy as _
from ckeditor.fields import RichTextField
from embed_video.fields import EmbedVideoField
from numberedmodel.models import NumberedModel
class VarCharField(models.TextField):
def formfield(self, **kwargs):
kwargs.update({'widget': TextInput})
return super().formfield(**kwargs)
# From https://github.com/JaapJoris/django-numberedmodel.git
class NumberedModel(models.Model):
def number_with_respect_to(self):
return self.__class__.objects.all()
def _renumber(self):
'''Renumbers the queryset while preserving the instance's number'''
queryset = self.number_with_respect_to()
field_name = self.__class__._meta.ordering[-1].lstrip('-')
this_nr = getattr(self, field_name)
if this_nr is None:
this_nr = len(queryset) + 1
# The algorithm: loop over the queryset and set each object's
# number to the counter. When an object's number equals the
# number of this instance, set this instance's number to the
# counter, increment the counter by 1, and finish the loop
counter = 1
inserted = False
for other in queryset.exclude(pk=self.pk):
other_nr = getattr(other, field_name)
if counter >= this_nr and not inserted:
setattr(self, field_name, counter)
inserted = True
counter += 1
if other_nr != counter:
setattr(other, field_name, counter)
super(NumberedModel, other).save()
counter += 1
if not inserted:
setattr(self, field_name, counter)
def save(self, *args, **kwargs):
self._renumber()
super(NumberedModel, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
super(NumberedModel, self).delete(*args, **kwargs)
self._renumber()
class Meta:
abstract = True
class Page(NumberedModel):
position = models.PositiveIntegerField(_('position'), blank=True)
title = models.CharField(_('title'), max_length=255)

Wyświetl plik

@ -0,0 +1 @@
default_app_config = 'numberedmodel.apps.NumberedModelConfig'

Wyświetl plik

@ -0,0 +1,4 @@
from django.apps import AppConfig
class NumberedModelConfig(AppConfig):
name = 'numberedmodel'

Wyświetl plik

@ -0,0 +1,44 @@
from django.db import models
class NumberedModel(models.Model):
def number_with_respect_to(self):
return self.__class__.objects.all()
def _renumber(self):
'''Renumbers the queryset while preserving the instance's number'''
queryset = self.number_with_respect_to()
field_name = self.__class__._meta.ordering[-1].lstrip('-')
this_nr = getattr(self, field_name)
if this_nr is None:
this_nr = len(queryset) + 1
# The algorithm: loop over the queryset and set each object's
# number to the counter. When an object's number equals the
# number of this instance, set this instance's number to the
# counter, increment the counter by 1, and finish the loop
counter = 1
inserted = False
for other in queryset.exclude(pk=self.pk):
other_nr = getattr(other, field_name)
if counter >= this_nr and not inserted:
setattr(self, field_name, counter)
inserted = True
counter += 1
if other_nr != counter:
setattr(other, field_name, counter)
super(NumberedModel, other).save()
counter += 1
if not inserted:
setattr(self, field_name, counter)
def save(self, *args, **kwargs):
self._renumber()
super(NumberedModel, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
super(NumberedModel, self).delete(*args, **kwargs)
self._renumber()
class Meta:
abstract = True

Wyświetl plik

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

6
requirements.txt 100644
Wyświetl plik

@ -0,0 +1,6 @@
Django
django-ckeditor
django-extensions
django-embed-video
easy-thumbnails
psycopg2 --no-binary psycopg2

Wyświetl plik

Wyświetl plik

@ -0,0 +1,25 @@
import os
from django.conf import settings
from sass import compile
class SimpleSassMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if settings.DEBUG and request.path.endswith('.css'):
_, staticdir, app, css_file = request.path.split('/', maxsplit=3)
sass_file = css_file[:-4]
css_path = os.path.join(app, staticdir, app, css_file)
sass_path = os.path.join(app, staticdir, app, sass_file)
map_path = css_path + '.map'
if os.path.exists(sass_path):
css = compile(filename=sass_path, output_style='nested')
css, mapping = compile(filename=sass_path, source_map_filename=map_path)
with open(css_path, 'w') as f:
f.write(css)
with open(map_path, 'w') as f:
f.write(mapping)
response = self.get_response(request)
return response