thinkserver/thinks/models.py
Christian Lawson-Perfect 500eb38774 lots of changes
Added a creation_time field to thinks - using the modified time on the
  filesystem wasn't reliable.

Styled the login page.

The index shows the most recent thinks at the top.

Allow authentication by an Authorization header, with a token specified
in settings.py.

Lots of improvements to the editor, including showing the log, and a
form to install Elm packages when editing .elm files.

The Makefile for a project is automatically run each time a file is saved.
2025-02-07 07:08:01 +00:00

69 lines
1.9 KiB
Python

from django.conf import settings
from django.db import models
from django.urls import reverse
from django.utils.timezone import datetime, make_aware
THINKS_DIR = settings.THINKS_DIR
# Create your models here.
class Think(models.Model):
slug = models.SlugField()
category = models.CharField(max_length=100, blank=True, null=True)
creation_time = models.DateTimeField(auto_now_add=True)
is_template = models.BooleanField(default=False)
class Meta:
ordering = ('slug',)
@property
def root(self):
return (THINKS_DIR / self.slug).resolve()
def get_absolute_url(self):
return reverse('think', kwargs={'slug': self.slug})
def get_static_url(self):
return settings.THINKS_STATIC_URL.format(slug=self.slug)
def files(self):
for f in self.root.iterdir():
yield f.relative_to(self.root)
def file_path(self, relpath):
if relpath is None:
return None
path = (self.root / relpath).resolve()
if not path.is_relative_to(self.root):
raise Exception(f"Bad path {path}")
return path
def get_readme(self):
readme_path = self.file_path('README')
for suffix in ['', '.md', '.rst', '.txt']:
p = readme_path.with_suffix(suffix)
if p.exists():
with open(p) as f:
return f.read()
return None
def get_log(self):
log_file = self.file_path('.make.log')
if not log_file.exists():
return ''
with open(log_file) as f:
log = f.read()
return log
def as_json(self):
return {
'slug': self.slug,
'category': self.category,
'absolute_url': self.get_absolute_url(),
'readme': self.get_readme(),
'creation_time': self.creation_time,
}