-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make new static_page table and admin view
- Loading branch information
1 parent
9b63d57
commit 147df74
Showing
9 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.shortcuts import render | ||
|
||
# Create your views here. |