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 untrusted user: 00dani
GPG key ID: 8EB789DDF3ABD240
3 changed files with 57 additions and 16 deletions

View file

@ -43,8 +43,8 @@
{% endfor %}
{% for s in entry.syndications.all() %}
{{i}}<a class="u-syndication card-link" href="{{ s.url }}">
{{i}}<i class="{{ s.profile.site.icon }}" aria-hidden="true"></i>
{{i}}{{ s.profile }}
{{i}}<i class="{{ s.site.icon }}" aria-hidden="true"></i>
{{i}}{{ s.site.domain }}
{{i}}</a>
{% endfor %}
{{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.sites.models import Site
from django.contrib.sites.models import Site as DjangoSite
from django.db import models
from django.urls import reverse
from django.utils.functional import cached_property
from itertools import groupby
from mf2util import interpret
from slugify import slugify
from textwrap import shorten
from urllib.parse import urljoin
from urllib.parse import urljoin, urlparse
from lemonshort.short_url import short_url
from meta.models import ModelMeta
from model_utils.models import TimeStampedModel
from users.models import Profile
from users.models import Site
from . import kinds
from lemoncurry import requests, utils
@ -141,7 +143,7 @@ class Entry(ModelMeta, TimeStampedModel):
@property
def absolute_url(self):
base = 'https://' + Site.objects.get_current().domain
base = 'https://' + DjangoSite.objects.get_current().domain
return urljoin(base, self.url)
@property
@ -162,7 +164,7 @@ class Entry(ModelMeta, TimeStampedModel):
@property
def json_ld(self):
base = 'https://' + Site.objects.get_current().domain
base = 'https://' + DjangoSite.objects.get_current().domain
url = urljoin(base, self.url)
posting = {
@ -190,21 +192,31 @@ class Entry(ModelMeta, TimeStampedModel):
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):
objects = SyndicationManager()
entry = models.ForeignKey(
Entry,
related_name='syndications',
on_delete=models.CASCADE
)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
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:
ordering = ['profile']
ordering = ['domain']