Advertisement
pacho_the_python

Untitled

Jul 31st, 2024
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. import os
  2. import django
  3.  
  4. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "orm_skeleton.settings")
  5. django.setup()
  6.  
  7. from django.db.models import Q, Count, Avg
  8. from main_app.models import Author, Article, Review
  9.  
  10.  
  11. def get_authors(search_name=None, search_email=None):
  12.     if search_name is not None and search_email is not None:
  13.         authors = Author.objects.filter(
  14.             Q(full_name__icontains=search_name) &
  15.             Q(email__icontains=search_email)
  16.         )
  17.     elif search_name is not None:
  18.         authors = Author.objects.filter(full_name__icontains=search_name)
  19.     elif search_email is not None:
  20.         authors = Author.objects.filter(email__icontains=search_email)
  21.     else:
  22.         return ""
  23.  
  24.     if not authors.exists():
  25.         return ""
  26.  
  27.     authors = authors.order_by('-full_name')
  28.     result = []
  29.  
  30.     for author in authors:
  31.         status = 'Banned' if author.is_banned else 'Not Banned'
  32.         result.append(f"Author: {author.full_name}, email: {author.email}, status: {status}")
  33.  
  34.     return "\n".join(result)
  35.  
  36.  
  37. def get_top_publisher():
  38.     top_publisher = Author.objects.annotate(num_of_articles=Count('article')).\
  39.         order_by('-num_of_articles', 'email').first()
  40.  
  41.     if top_publisher and top_publisher.num_of_articles > 0:
  42.         return f"Top Author: {top_publisher.full_name} with {top_publisher.num_of_articles} published articles."
  43.     return ""
  44.  
  45.  
  46. def get_top_reviewer():
  47.     top_reviewer = Author.objects.annotate(num_of_reviews=Count('review')).order_by('-num_of_reviews', 'email').first()
  48.  
  49.     if top_reviewer and top_reviewer.num_of_reviews > 0:
  50.         return f"Top Reviewer: {top_reviewer.full_name} with {top_reviewer.num_of_reviews} published reviews."
  51.     return ""
  52.  
  53.  
  54. def get_latest_article():
  55.     latest_article = Article.objects.order_by('-published_on').first()
  56.  
  57.     if not latest_article:
  58.         return ""
  59.  
  60.     authors = latest_article.authors.order_by('full_name')
  61.     author_names = ", ".join([author.full_name for author in authors])
  62.     num_reviews = Review.objects.filter(article=latest_article).count()
  63.     avg_rating = Review.objects.filter(article=latest_article).aggregate(Avg('rating'))['rating__avg'] or 0
  64.  
  65.     return f"The latest article is: {latest_article.title}. Authors: {author_names}. " \
  66.            f"Reviewed: {num_reviews} times. Average Rating: {avg_rating:.2f}."
  67.  
  68.  
  69. def get_top_rated_article():
  70.     top_rated_article = Article.objects.annotate(avg_rating=Avg('review__rating')).\
  71.         order_by('-avg_rating', 'title').first()
  72.  
  73.     if not top_rated_article or top_rated_article.avg_rating is None:
  74.         return ""
  75.  
  76.     num_reviews = Review.objects.filter(article=top_rated_article).count()
  77.     avg_rating = top_rated_article.avg_rating
  78.  
  79.     return f"The top-rated article is: {top_rated_article.title}, with an average rating of {avg_rating:.2f}, " \
  80.            f"reviewed {num_reviews} times."
  81.  
  82.  
  83. def ban_author(email=None):
  84.     if email is None:
  85.         return "No authors banned."
  86.  
  87.     try:
  88.         author = Author.objects.get(email=email)
  89.     except Author.DoesNotExist:
  90.         return "No authors banned."
  91.  
  92.     num_reviews = Review.objects.filter(author=author).count()
  93.     Review.objects.filter(author=author).delete()
  94.     author.is_banned = True
  95.     author.save()
  96.  
  97.     return f"Author: {author.full_name} is banned! {num_reviews} reviews deleted."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement