data: add uuid to Page model (#67710)
This commit is contained in:
parent
5f90155ba6
commit
421cce8d82
|
@ -0,0 +1,16 @@
|
|||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('data', '0059_textcell_title'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='page',
|
||||
name='uuid',
|
||||
field=models.UUIDField(editable=False, null=True),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,54 @@
|
|||
import hashlib
|
||||
import uuid
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def natural_key(page):
|
||||
def get_parents_and_self(page):
|
||||
pages = [page]
|
||||
_page = page
|
||||
while _page.parent_id:
|
||||
_page = _page.parent
|
||||
pages.append(_page)
|
||||
return list(reversed(pages))
|
||||
|
||||
def get_online_url(page):
|
||||
parts = [x.slug for x in get_parents_and_self(page)]
|
||||
if parts[0] == 'index':
|
||||
parts = parts[1:]
|
||||
if not parts:
|
||||
return '/'
|
||||
return '/' + '/'.join(parts) + '/'
|
||||
|
||||
return get_online_url(page).strip('/')
|
||||
|
||||
|
||||
def forward(apps, schema_editor):
|
||||
Page = apps.get_model('data', 'Page')
|
||||
known_uuids = set(Page.objects.filter(uuid__isnull=False).values_list('uuid', flat=True))
|
||||
for page in Page.objects.filter(uuid__isnull=True):
|
||||
if page.snapshot is not None:
|
||||
page.uuid = uuid.uuid4()
|
||||
page.save()
|
||||
known_uuids.add(page.uuid)
|
||||
continue
|
||||
slug = natural_key(page) or 'index'
|
||||
slug_hash = hashlib.sha256(slug.encode('utf-8'))
|
||||
page.uuid = uuid.UUID(slug_hash.hexdigest()[:32])
|
||||
if page.uuid in known_uuids:
|
||||
# uuid unicity !
|
||||
page.uuid = uuid.uuid4()
|
||||
known_uuids.add(page.uuid)
|
||||
page.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('data', '0060_page_uuid'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(forward, reverse_code=migrations.RunPython.noop),
|
||||
]
|
|
@ -0,0 +1,18 @@
|
|||
import uuid
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('data', '0061_page_uuid'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='page',
|
||||
name='uuid',
|
||||
field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True),
|
||||
),
|
||||
]
|
|
@ -24,6 +24,7 @@ import os
|
|||
import re
|
||||
import subprocess
|
||||
import urllib.parse
|
||||
import uuid
|
||||
|
||||
import feedparser
|
||||
import requests
|
||||
|
@ -195,6 +196,7 @@ class Page(models.Model):
|
|||
objects = PageManager()
|
||||
snapshots = PageManager(snapshots=True)
|
||||
|
||||
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
|
||||
title = models.CharField(_('Title'), max_length=150)
|
||||
slug = models.SlugField(_('Slug'))
|
||||
sub_slug = models.CharField(
|
||||
|
|
Loading…
Reference in New Issue