Have syndications infer the correct Site from their URL rather than require an explicit Profile reference

This commit is contained in:
Danielle McLean 2018-06-28 20:51:43 +10:00
parent ac22c826cb
commit 556329d5fa
Signed by: 00dani
GPG Key ID: 8EB789DDF3ABD240
3 changed files with 57 additions and 16 deletions

View File

@ -43,8 +43,8 @@
{% endfor %} {% endfor %}
{% for s in entry.syndications.all() %} {% for s in entry.syndications.all() %}
{{i}}<a class="u-syndication card-link" href="{{ s.url }}"> {{i}}<a class="u-syndication card-link" href="{{ s.url }}">
{{i}}<i class="{{ s.profile.site.icon }}" aria-hidden="true"></i> {{i}}<i class="{{ s.site.icon }}" aria-hidden="true"></i>
{{i}}{{ s.profile }} {{i}}{{ s.site.domain }}
{{i}}</a> {{i}}</a>
{% endfor %} {% endfor %}
{{i}}</div> {{i}}</div>

View File

@ -0,0 +1,29 @@
# Generated by Django 2.0.6 on 2018-06-28 10:44
import computed_property.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('entries', '0011_auto_20171120_1108'),
]
operations = [
migrations.AlterModelOptions(
name='syndication',
options={'ordering': ['domain']},
),
migrations.RemoveField(
model_name='syndication',
name='profile',
),
migrations.AddField(
model_name='syndication',
name='domain',
field=computed_property.fields.ComputedCharField(
compute_from='calc_domain', default='', editable=False, max_length=255),
preserve_default=False,
),
]

View File

@ -1,17 +1,19 @@
from computed_property import ComputedCharField
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site from django.contrib.sites.models import Site as DjangoSite
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils.functional import cached_property
from itertools import groupby from itertools import groupby
from mf2util import interpret from mf2util import interpret
from slugify import slugify from slugify import slugify
from textwrap import shorten from textwrap import shorten
from urllib.parse import urljoin from urllib.parse import urljoin, urlparse
from lemonshort.short_url import short_url from lemonshort.short_url import short_url
from meta.models import ModelMeta from meta.models import ModelMeta
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
from users.models import Profile from users.models import Site
from . import kinds from . import kinds
from lemoncurry import requests, utils from lemoncurry import requests, utils
@ -141,7 +143,7 @@ class Entry(ModelMeta, TimeStampedModel):
@property @property
def absolute_url(self): def absolute_url(self):
base = 'https://' + Site.objects.get_current().domain base = 'https://' + DjangoSite.objects.get_current().domain
return urljoin(base, self.url) return urljoin(base, self.url)
@property @property
@ -162,7 +164,7 @@ class Entry(ModelMeta, TimeStampedModel):
@property @property
def json_ld(self): def json_ld(self):
base = 'https://' + Site.objects.get_current().domain base = 'https://' + DjangoSite.objects.get_current().domain
url = urljoin(base, self.url) url = urljoin(base, self.url)
posting = { posting = {
@ -190,21 +192,31 @@ class Entry(ModelMeta, TimeStampedModel):
ordering = ['-created'] ordering = ['-created']
class SyndicationManager(models.Manager):
def get_queryset(self):
qs = super(SyndicationManager, self).get_queryset()
return qs.select_related('profile__site')
class Syndication(models.Model): class Syndication(models.Model):
objects = SyndicationManager()
entry = models.ForeignKey( entry = models.ForeignKey(
Entry, Entry,
related_name='syndications', related_name='syndications',
on_delete=models.CASCADE on_delete=models.CASCADE
) )
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
url = models.CharField(max_length=255) url = models.CharField(max_length=255)
domain = ComputedCharField(
compute_from='calc_domain', max_length=255,
)
def calc_domain(self):
domain = urlparse(self.url).netloc
if domain.startswith('www.'):
domain = domain[4:]
return domain
@cached_property
def site(self):
d = self.domain
try:
return Site.objects.get(domain=d)
except Site.DoesNotExist:
return Site(name=d, domain=d, icon='fas fa-newspaper')
class Meta: class Meta:
ordering = ['profile'] ordering = ['domain']