Skip to content

Instantly share code, notes, and snippets.

@juan-fdz-hawa
Last active October 25, 2021 13:54
Show Gist options
  • Save juan-fdz-hawa/03cc1c120991b735205a720fd49a7ee4 to your computer and use it in GitHub Desktop.
Save juan-fdz-hawa/03cc1c120991b735205a720fd49a7ee4 to your computer and use it in GitHub Desktop.
from django.core.management.base import BaseCommand
from search_seo.models import SeoTemplate
from itertools import product
from django.apps import apps
import os
import time
class Command(BaseCommand):
help = "Migrate location category content to the db"
def handle(self, *args, **options):
SeoTemplate.objects.all().delete()
root = apps.get_containing_app_config("search_seo").path
area_types = (
"country",
"administrative_area_level_1",
"locality",
"sublocality",
"natural_feature",
"category",
)
for f_parts in product(
(root + "/templates/search",), ("com", "test"), area_types
):
templates = []
for root, dirs, files in os.walk("/".join(f_parts)):
for d in dirs:
seo_template = {
"area_type": f_parts[2],
"category_id": d,
"tld": f_parts[1],
}
for template in ("description", "h1", "seo_text", "title"):
f = open(
os.path.join("/".join(f_parts), f"{d}", f"{template}.html"),
"r",
)
seo_template[template] = f.read()
f.close()
time.sleep(2)
templates.append(SeoTemplate(**seo_template))
SeoTemplate.objects.bulk_create(templates)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment