Skip to content

Commit

Permalink
Migrate web server back-end from Flask to Django (#2040)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicomiguelino authored Oct 16, 2024
1 parent e3cfeb1 commit 188e399
Show file tree
Hide file tree
Showing 81 changed files with 2,562 additions and 2,756 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[flake8]
exclude =
anthias_app/migrations/*.py
per-file-ignores =
bin/migrate.py: E501
9 changes: 6 additions & 3 deletions .github/workflows/docker-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ jobs:
run: |
docker compose -f docker-compose.test.yml up -d
- name: Run the tests inside the container
shell: 'script -q -e -c "bash {0}"'
- name: Run the unit tests inside the container
run: |
docker compose -f docker-compose.test.yml exec anthias-test ./manage.py test --noinput --parallel --exclude-tag=integration
- name: Run the integration tests inside the container
run: |
docker compose -f docker-compose.test.yml exec anthias-test bash ./bin/prepare_test_environment.sh -s
docker compose -f docker-compose.test.yml exec anthias-test nose2 -v
docker compose -f docker-compose.test.yml exec anthias-test ./manage.py test --noinput --tag=integration
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ docker/Dockerfile.test
docker-compose.yml

balena-deploy/
db.sqlite3

# Django
staticfiles/
3 changes: 3 additions & 0 deletions anthias_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin # noqa F401

# Register your models here.
6 changes: 6 additions & 0 deletions anthias_app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AnthiasAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'anthias_app'
59 changes: 31 additions & 28 deletions anthias_app/helpers.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import uuid
import yaml
from datetime import datetime
from flask import render_template
from os import getenv, path

from lib import assets_helper, db
from django.shortcuts import render
from django.utils import timezone
from lib.github import is_up_to_date
from lib.utils import get_video_duration
from os import getenv, path
from anthias_app.models import Asset
from settings import settings


def template(template_name, **context):
def template(request, template_name, context):
"""
This is a helper function that is used to render a template
with some global context. This is used to avoid having to
repeat code in other views.
"""
This is a template response wrapper that shares the
same function signature as Flask's render_template() method
but also injects some global context."""

# Add global contexts
context['date_format'] = settings['date_format']
context['default_duration'] = settings['default_duration']
context['default_streaming_duration'] = (
settings['default_streaming_duration'])
settings['default_streaming_duration']
)
context['template_settings'] = {
'imports': ['from lib.utils import template_handle_unicode'],
'default_filters': ['template_handle_unicode'],
}
context['up_to_date'] = is_up_to_date()
context['use_24_hour_clock'] = settings['use_24_hour_clock']

return render_template(template_name, context=context)
return render(request, template_name, context)


def prepare_default_asset(**kwargs):
Expand All @@ -46,7 +47,6 @@ def prepare_default_asset(**kwargs):
'asset_id': asset_id,
'duration': duration,
'end_date': kwargs['end_date'],
'is_active': 1,
'is_enabled': True,
'is_processing': 0,
'mimetype': kwargs['mimetype'],
Expand All @@ -62,33 +62,36 @@ def prepare_default_asset(**kwargs):
def add_default_assets():
settings.load()

datetime_now = datetime.now()
datetime_now = timezone.now()
default_asset_settings = {
'start_date': datetime_now,
'end_date': datetime_now.replace(year=datetime_now.year + 6),
'duration': settings['default_duration']
}

default_assets_yaml = path.join(
getenv('HOME'), '.screenly/default_assets.yml')
getenv('HOME'),
'.screenly/default_assets.yml',
)

with open(default_assets_yaml, 'r') as yaml_file:
default_assets = yaml.safe_load(yaml_file).get('assets')
with db.conn(settings['database']) as conn:
for default_asset in default_assets:
default_asset_settings.update({
'name': default_asset.get('name'),
'uri': default_asset.get('uri'),
'mimetype': default_asset.get('mimetype')
})
asset = prepare_default_asset(**default_asset_settings)
if asset:
assets_helper.create(conn, asset)

for default_asset in default_assets:
default_asset_settings.update({
'name': default_asset.get('name'),
'uri': default_asset.get('uri'),
'mimetype': default_asset.get('mimetype')
})
asset = prepare_default_asset(**default_asset_settings)

if asset:
Asset.objects.create(**asset)


def remove_default_assets():
settings.load()
with db.conn(settings['database']) as conn:
for asset in assets_helper.read(conn):
if asset['asset_id'].startswith('default_'):
assets_helper.delete(conn, asset['asset_id'])

for asset in Asset.objects.all():
if asset.asset_id.startswith('default_'):
asset.delete()
36 changes: 36 additions & 0 deletions anthias_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 3.2.18 on 2024-08-23 18:45

import anthias_app.models
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Asset',
fields=[
('asset_id', models.TextField(default=anthias_app.models.generate_asset_id, editable=False, primary_key=True, serialize=False)),
('name', models.TextField(blank=True, null=True)),
('uri', models.TextField(blank=True, null=True)),
('md5', models.TextField(blank=True, null=True)),
('start_date', models.DateTimeField(blank=True, null=True)),
('end_date', models.DateTimeField(blank=True, null=True)),
('duration', models.TextField(blank=True, null=True)),
('mimetype', models.TextField(blank=True, null=True)),
('is_enabled', models.IntegerField(default=0)),
('is_processing', models.IntegerField(default=0)),
('nocache', models.IntegerField(default=0)),
('play_order', models.IntegerField(default=0)),
('skip_asset_check', models.IntegerField(default=0)),
],
options={
'db_table': 'assets',
},
),
]
38 changes: 38 additions & 0 deletions anthias_app/migrations/0002_auto_20241015_1524.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 3.2.18 on 2024-10-15 15:24

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('anthias_app', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='asset',
name='duration',
field=models.BigIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='asset',
name='is_enabled',
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name='asset',
name='is_processing',
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name='asset',
name='nocache',
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name='asset',
name='skip_asset_check',
field=models.BooleanField(default=False),
),
]
Empty file.
36 changes: 36 additions & 0 deletions anthias_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import uuid
from django.db import models
from django.utils import timezone


def generate_asset_id():
return uuid.uuid4().hex


class Asset(models.Model):
asset_id = models.TextField(
primary_key=True, default=generate_asset_id, editable=False)
name = models.TextField(blank=True, null=True)
uri = models.TextField(blank=True, null=True)
md5 = models.TextField(blank=True, null=True)
start_date = models.DateTimeField(blank=True, null=True)
end_date = models.DateTimeField(blank=True, null=True)
duration = models.BigIntegerField(blank=True, null=True)
mimetype = models.TextField(blank=True, null=True)
is_enabled = models.BooleanField(default=False)
is_processing = models.BooleanField(default=False)
nocache = models.BooleanField(default=False)
play_order = models.IntegerField(default=0)
skip_asset_check = models.BooleanField(default=False)

class Meta:
db_table = 'assets'

def is_active(self):
if self.is_enabled and self.start_date and self.end_date:
current_time = timezone.now()
return (
self.start_date < current_time < self.end_date
)

return False
3 changes: 3 additions & 0 deletions anthias_app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase # noqa F401

# Create your tests here.
12 changes: 12 additions & 0 deletions anthias_app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import path
from . import views

app_name = 'anthias_app'

urlpatterns = [
path('', views.index, name='index'),
path('settings', views.settings_page, name='settings'),
path('system-info', views.system_info, name='system_info'),
path('integrations', views.integrations, name='integrations'),
path('splash-page', views.splash_page, name='splash_page'),
]
Loading

0 comments on commit 188e399

Please sign in to comment.