Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Page Model & UI #419

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions daras_ai_v2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"handles",
"payments",
"functions",
"static_pages",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -182,6 +183,7 @@
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
"default": {},
}

# Default primary key field type
Expand Down Expand Up @@ -234,6 +236,32 @@
GOOGLE_CLIENT_ID = config("GOOGLE_CLIENT_ID", default="")
FIREBASE_CONFIG = config("FIREBASE_CONFIG", default="")


# django-storages settings for google-cloud
from google.oauth2 import service_account

GCS_PRIVATE_KEY = config("GCS_PRIVATE_KEY", default="")

GCS_CREDENTIALS = ""
# create credentials from env vars
try:
GCS_CONFIG = config("GCS_CONFIG")
except UndefinedValueError:
pass
else:
GCS_CREDENTIALS = service_account.Credentials.from_service_account_info(
info={**json.loads(GCS_CONFIG), "private_key": GCS_PRIVATE_KEY}
)

STORAGES["default"] = {
"BACKEND": "storages.backends.gcloud.GoogleCloudStorage",
"OPTIONS": {
"project_id": GCP_PROJECT,
"bucket_name": GS_BUCKET_NAME,
"credentials": GCS_CREDENTIALS,
},
}

UBERDUCK_KEY = config("UBERDUCK_KEY", None)
UBERDUCK_SECRET = config("UBERDUCK_SECRET", None)

Expand Down
89 changes: 89 additions & 0 deletions daras_ai_v2/static_pages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import io
from starlette.responses import Response
from static_pages.models import StaticPage
from google.cloud import storage

from bs4 import BeautifulSoup
from daras_ai_v2.settings import GCP_PROJECT, GCS_CREDENTIALS, GS_BUCKET_NAME


def gcs_bucket() -> "storage.storage.Bucket":
client = storage.Client(
GCP_PROJECT,
GCS_CREDENTIALS,
)
bucket = client.get_bucket(GS_BUCKET_NAME)
return bucket


def populate_imported_css(html: str, uid: str):
soup = BeautifulSoup(html, "html.parser")
links = soup.find_all("link")
hrefList = [link.get("href") for link in links if link.get("href") is not None]
# Remove all <link> tags
for link in links:
link.decompose()

styles = get_all_styles(hrefList, uid)
style_tag = BeautifulSoup(styles, "html.parser").style
# Insert the style tag into the body
if soup.body:
soup.body.insert(0, style_tag)
else:
# If body tag does not exist, create it and add style tag
body_tag = soup.new_tag("body")
body_tag.insert(0, style_tag)
soup.append(body_tag)

return soup


def get_all_styles(links: list, uid: str):
styles = ""
for link in links:
if not link.endswith(".css"): # ignore for css files
continue
blob = gcs_bucket().get_blob(f"{uid}/{link}")
blob = blob.download_as_string()
blob = blob.decode("utf-8")
blob = io.StringIO(blob).read()
styles += blob

return f"<style>{styles}</style>"


def serve(request: Response, page_slug: str):
static_page = StaticPage.objects.get(path=page_slug)
if not static_page:
return None

file_path = request.url.path
if not file_path == f"/{page_slug}/":
file_path = file_path[len(f"/{page_slug}/") :]
else:
file_path = None

uid = static_page.uid
bucket = gcs_bucket()

def render_page():
if file_path:
return None
html = None
blob = bucket.get_blob(f"{uid}/index.html")
blob = blob.download_as_string()
blob = blob.decode("utf-8")
html = io.StringIO(blob).read()
withStyleHtml = populate_imported_css(
html, uid
) # Add all the styles in the html

return withStyleHtml

def get_file_url():
if not file_path:
return None
STATIC_URL = f"https://storage.googleapis.com/gooey-test/{uid}"
return f"{STATIC_URL}/{file_path}"

return render_page(), get_file_url()
46 changes: 45 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ azure-cognitiveservices-speech = "^1.37.0"
twilio = "^9.2.3"
sentry-sdk = {version = "1.45.0", extras = ["loguru"]}
gooey-gui = "^0.1.0"
django-storages = "^1.14.4"

[tool.poetry.group.dev.dependencies]
watchdog = "^2.1.9"
Expand Down
26 changes: 24 additions & 2 deletions routers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
PlainTextResponse,
Response,
FileResponse,
HTMLResponse,
)

import gooey_gui as gui
Expand All @@ -43,6 +44,8 @@
from daras_ai_v2.query_params_util import extract_query_params
from daras_ai_v2.settings import templates
from handles.models import Handle
from daras_ai_v2.functional import R
from static_pages.models import StaticPage

app = APIRouter()

Expand Down Expand Up @@ -575,15 +578,34 @@ def chat_lib_route(request: Request, integration_id: str, integration_name: str
app,
"/{page_slug}/",
"/{page_slug}/{run_slug}/",
"/{page_slug}/{run_slug}/{path}",
"/{page_slug}/{run_slug}-{example_id}/",
)
def recipe_page_or_handle(
request: Request, page_slug: str, run_slug: str = None, example_id: str = None
request: Request,
page_slug: str,
run_slug: str = None,
path: str = None,
example_id: str = None,
):
try:
handle = Handle.objects.get_by_name(page_slug)
except Handle.DoesNotExist:
return render_page(request, page_slug, RecipeTabs.run, example_id)
try:
import daras_ai_v2.static_pages as static_pages

html, file_path = static_pages.serve(
request=request,
page_slug=page_slug,
)

if file_path:
return RedirectResponse(file_path)
elif html:
return HTMLResponse(html)
except StaticPage.DoesNotExist:
return render_page(request, page_slug, RecipeTabs.run, example_id)

else:
return render_page_for_handle(request, handle)

Expand Down
2 changes: 1 addition & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
app.include_router(account.app, include_in_schema=False)
app.include_router(facebook_api.app, include_in_schema=False)
app.include_router(slack_api.router, include_in_schema=False)
app.include_router(root.app, include_in_schema=False)
app.include_router(url_shortener.app, include_in_schema=False)
app.include_router(paypal.router, include_in_schema=False)
app.include_router(stripe.router, include_in_schema=False)
app.include_router(twilio_api.router, include_in_schema=False)
app.include_router(root.app, include_in_schema=False) # this has a catch-all route

app.add_middleware(
CORSMiddleware,
Expand Down
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.
Loading