Skip to content
Open
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
32 changes: 32 additions & 0 deletions blog/migrations/0007_blogcategory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 5.1.2 on 2024-10-24 15:44

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("blog", "0006_rename_authors_blogpage_author"),
]

operations = [
migrations.CreateModel(
name="BlogCategory",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255)),
("slug", models.SlugField(unique=True)),
],
options={
"verbose_name_plural": "Categories",
},
),
]
48 changes: 48 additions & 0 deletions blog/migrations/0008_blogpagecategory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 5.1.2 on 2024-10-24 15:48

import django.db.models.deletion
import modelcluster.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("blog", "0007_blogcategory"),
]

operations = [
migrations.CreateModel(
name="BlogPageCategory",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"category",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="blog_pages",
to="blog.blogcategory",
),
),
(
"page",
modelcluster.fields.ParentalKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="categories",
to="blog.blogpage",
),
),
],
options={
"verbose_name_plural": "Categories",
},
),
]
4 changes: 2 additions & 2 deletions home/migrations/0005_alter_standardpage_body.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.2 on 2024-10-28 14:23
# Generated by Django 5.1.2 on 2024-10-25 11:06

import wagtail.fields
from django.db import migrations
Expand All @@ -15,7 +15,7 @@ class Migration(migrations.Migration):
model_name="standardpage",
name="body",
field=wagtail.fields.StreamField(
[("paragraph", 0)],
[("paragrpah", 0)],
blank=True,
block_lookup={0: ("wagtail.blocks.RichTextBlock", (), {})},
),
Expand Down
9 changes: 3 additions & 6 deletions home/migrations/0006_alter_standardpage_body.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.2 on 2024-10-28 15:21
# Generated by Django 5.1.2 on 2024-10-25 11:50

import wagtail.fields
from django.db import migrations
Expand All @@ -15,12 +15,9 @@ class Migration(migrations.Migration):
model_name="standardpage",
name="body",
field=wagtail.fields.StreamField(
[("paragraph", 1)],
[("paragraph", 0)],
blank=True,
block_lookup={
0: ("wagtail.blocks.RichTextBlock", (), {}),
1: ("wagtail.blocks.StructBlock", [[("content", 0)]], {}),
},
block_lookup={0: ("wagtail.blocks.RichTextBlock", (), {})},
),
),
]
2 changes: 2 additions & 0 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from home.blocks import StreamBlocks
from wp_connector.field_panels import WordpressInfoPanel

from .blocks import StreamBlock


class HomePage(Page):
pass
Expand Down
4 changes: 3 additions & 1 deletion wp_connector/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,10 @@ def update_wagtail_page(self, admin, request, queryset):
for obj in queryset:
if not obj.wagtail_page_id:
# skip objects that do not have a wagtail_page_id
message_param = obj.title or obj.wp_id
self.handle_message_user(
request,
f"Page doen't exist for {obj.title} yet. Use the 'Create New Wagtail Pages' action",
f"Page doesn't exist for {message_param} yet. Use the 'Create New Wagtail Pages' action",
level="WARNING",
)
continue
Expand All @@ -454,6 +455,7 @@ def update_wagtail_page(self, admin, request, queryset):
exporter.post_init_messages["message"],
level=exporter.post_init_messages["level"],
)

if exporter.post_init_messages.get("skip", False):
continue

Expand Down
32 changes: 31 additions & 1 deletion wp_connector/exporter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from dataclasses import dataclass
import json
from dataclasses import dataclass, field
from typing import List

from bs4 import BeautifulSoup as bs
from django.apps import apps
from taggit.models import Tag

Expand Down Expand Up @@ -53,6 +56,7 @@ class Exporter:
wagtail_page_model_has_author: bool = False
wagtail_page_model_has_tags: bool = False
wagtail_page_model_has_categories: bool = False
wagtail_page_model_streamfields: List = field(default_factory=list)
field_mapping: dict = None

# this takes precedence over the field_mapping
Expand Down Expand Up @@ -243,3 +247,29 @@ def do_update_wagtail_page(self):
"message": f"Updated wagtail page ID:{wagtail_page.id}",
"level": "SUCCESS",
}


@dataclass
class HTML_to_Streamfield:
"""
Convert HTML to Streamfield JSON
"""

html: str = ""

stream_data: List = field(default_factory=list)

def __post_init__(self):
self.stream_data = self.parse_html_to_streamfield_json()
print(self.stream_data)

def parse_html_to_streamfield_json(self):
soup = bs(self.html, "html.parser")
cache = []
for tag in soup.find_all(): # top level tags
if len(cache == 0):
cache.append({"type": "paragraph", "value": tag.text})
return cache

def get_stream_data(self):
return json.dumps(self.stream_data)
6 changes: 4 additions & 2 deletions wp_connector/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.2 on 2024-10-16 20:28
# Generated by Django 5.1.2 on 2024-10-23 20:36

import django.db.models.deletion
from django.db import migrations, models
Expand Down Expand Up @@ -32,7 +32,7 @@ class Migration(migrations.Migration):
("wagtail_model", models.JSONField(blank=True, null=True)),
("wp_cleaned_content", models.TextField(blank=True, null=True)),
("wp_block_content", models.JSONField(blank=True, null=True)),
("name", models.CharField(max_length=255)),
("name", models.CharField(help_text="Display name", max_length=255)),
("url", models.URLField(blank=True, null=True)),
("description", models.TextField(blank=True, null=True)),
("link", models.URLField()),
Expand Down Expand Up @@ -140,6 +140,7 @@ class Migration(migrations.Migration):
("wagtail_model", models.JSONField(blank=True, null=True)),
("wp_cleaned_content", models.TextField(blank=True, null=True)),
("wp_block_content", models.JSONField(blank=True, null=True)),
("wagtail_page_id", models.IntegerField(blank=True, null=True)),
("title", models.CharField(max_length=255)),
("date", models.DateTimeField()),
("date_gmt", models.DateTimeField()),
Expand Down Expand Up @@ -201,6 +202,7 @@ class Migration(migrations.Migration):
("wagtail_model", models.JSONField(blank=True, null=True)),
("wp_cleaned_content", models.TextField(blank=True, null=True)),
("wp_block_content", models.JSONField(blank=True, null=True)),
("wagtail_page_id", models.IntegerField(blank=True, null=True)),
("title", models.CharField(max_length=255)),
("date", models.DateTimeField()),
("date_gmt", models.DateTimeField()),
Expand Down
18 changes: 0 additions & 18 deletions wp_connector/migrations/0002_alter_wpauthor_name.py

This file was deleted.

This file was deleted.

10 changes: 8 additions & 2 deletions wp_connector/models/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def __init__(self, *args, **kwargs):
wagtail_model = models.JSONField(blank=True, null=True)
wp_cleaned_content = models.TextField(blank=True, null=True)
wp_block_content = models.JSONField(blank=True, null=True)
wagtail_page_id = models.IntegerField(blank=True, null=True)

# avoid errors with models not exportable in django admin
wagtail_page_id = models.NOT_PROVIDED

class Meta:
abstract = True
Expand Down Expand Up @@ -88,7 +90,11 @@ def get_source_url(self):
return self.SOURCE_URL.strip("/")


class ExportableMixin:
class ExportableWordpressModel(WordpressModel):
WAGTAIL_PAGE_MODEL = None # e.g. "blog.BlogPage"
WAGTAIL_PAGE_MODEL_PARENT = None # e.g. "blog.BlogIndexPage"
FIELD_MAPPING = {} # e.g. {"title": "title"} {[object_field]: [wagtail_field]}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not self.WAGTAIL_PAGE_MODEL:
Expand Down
1 change: 0 additions & 1 deletion wp_connector/models/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class WPPage(WordpressModel, ExportableMixin, StreamFieldMixin):
SOURCE_URL = "/wp-json/wp/v2/pages"
WAGTAIL_PAGE_MODEL = "home.StandardPage"
WAGTAIL_PAGE_MODEL_PARENT = "home.HomePage"

FIELD_MAPPING = {
"title": "title",
"content": "body",
Expand Down