alternative image from other post
This commit is contained in:
@@ -11,6 +11,9 @@ import re
|
||||
from rest_framework import serializers
|
||||
from django.db.models import Q
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger('posts')
|
||||
|
||||
class PostManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super().get_queryset()
|
||||
@@ -80,11 +83,27 @@ class Post(models.Model):
|
||||
if self.image:
|
||||
return self.image.url
|
||||
else:
|
||||
return ""
|
||||
image=self.find_an_image()
|
||||
if image:
|
||||
return image.url
|
||||
else:
|
||||
return ""
|
||||
|
||||
def key(self):
|
||||
return self.slug or self.id
|
||||
|
||||
def find_an_image(self):
|
||||
"find an image via another post"
|
||||
posts1=Post.objects.filter(slug__in=self.tags.names()).filter(image__isnull=False)[0:1].all() # Query all posts that have a slug that equals one of the tags
|
||||
if len(posts1)>0:
|
||||
return posts1.get().image
|
||||
|
||||
posts2=self.tags.similar_objects()
|
||||
for p in posts2:
|
||||
if p.image is not None:
|
||||
return p.image
|
||||
return None
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return reverse('posts.show', kwargs={"id":self.slug})
|
||||
|
||||
Reference in New Issue
Block a user