Skip to content

Commit

Permalink
make new static_page table and admin view
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-work committed Jul 29, 2024
1 parent 9b63d57 commit 147df74
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 0 deletions.
Empty file added static_pages/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions static_pages/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.contrib import admin
from .models import StaticPage


@admin.register(StaticPage)
class StaticPageAdmin(admin.ModelAdmin):
fieldsets = [
(
None,
{
"fields": [
"uid",
"name",
"path",
"zip_file",
"created_at",
"updated_at",
"disable_page_wrapper",
],
},
),
]
list_display = [
"name",
"path",
"created_at",
"updated_at",
"disable_page_wrapper",
]
list_filter = ["created_at", "updated_at", "disable_page_wrapper"]
search_fields = ["name", "path"]
readonly_fields = ["uid", "created_at", "updated_at"]
ordering = ["-updated_at"]
12 changes: 12 additions & 0 deletions static_pages/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.apps import AppConfig


class StaticPagesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "static_pages"
verbose_name = "Static Pages"

def ready(self):
from . import signals

return signals
30 changes: 30 additions & 0 deletions static_pages/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.7 on 2024-07-29 21:54

from django.db import migrations, models
import django.utils.timezone
import static_pages.models
import uuid


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='StaticPage',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('uid', models.CharField(default=uuid.UUID('142821e6-4df5-11ef-bbec-5e2196b992a2'), editable=False, unique=True)),
('name', models.CharField(max_length=100)),
('path', models.CharField(max_length=100)),
('zip_file', models.FileField(default=None, upload_to=static_pages.models.zip_file_upload_to)),
('created_at', models.DateTimeField(blank=True, default=django.utils.timezone.now, editable=False)),
('updated_at', models.DateTimeField(auto_now=True)),
('disable_page_wrapper', models.BooleanField(default=False)),
],
),
]
Empty file.
26 changes: 26 additions & 0 deletions static_pages/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.db import models
from django.utils import timezone
import uuid


def zip_file_upload_to(instance, filename):
# Use the instance's uid to generate the file path
return f"{instance.uid}/zips/{filename}"


class StaticPage(models.Model):
uid = models.CharField(default=uuid.uuid1(), editable=False, unique=True)
name = models.CharField(max_length=100)
path = models.CharField(max_length=100)
zip_file = models.FileField(upload_to=zip_file_upload_to, default=None)
created_at = models.DateTimeField(editable=False, blank=True, default=timezone.now)
updated_at = models.DateTimeField(auto_now=True)
disable_page_wrapper = models.BooleanField(default=False)

def save(self, *args, **kwargs):
if not self.uid:
self.uid = uuid.uuid1()
super().save(*args, **kwargs)

def __str__(self):
return self.name
22 changes: 22 additions & 0 deletions static_pages/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from daras_ai_v2.settings import GCP_PROJECT, GS_BUCKET_NAME, GCS_CREDENTIALS
from django.db.models.signals import post_save
from static_pages.models import StaticPage
from django.dispatch import receiver
from google.cloud import storage
import zipfile


@receiver(post_save, sender=StaticPage)
def unzip_static_page(sender, instance, **kwargs):
zip_file = instance.zip_file
if instance.zip_file:
with zipfile.ZipFile(zip_file, "r") as z:
client = storage.Client(GCP_PROJECT, GCS_CREDENTIALS)
bucket = client.get_bucket(GS_BUCKET_NAME)
for file_info in z.infolist():
if not file_info.is_dir():
file_data = z.read(file_info)
file_name = file_info.filename # Maintain directory structure
blob_path = f"{instance.uid}/{file_name}"
blob = bucket.blob(blob_path)
blob.upload_from_string(file_data)
3 changes: 3 additions & 0 deletions static_pages/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions static_pages/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.

0 comments on commit 147df74

Please sign in to comment.