Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import django
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "orm_skeleton.settings")
- django.setup()
- from django.db.models import Q, Count, Avg
- from main_app.models import Author, Article, Review
- def get_authors(search_name=None, search_email=None):
- if search_name is not None and search_email is not None:
- authors = Author.objects.filter(
- Q(full_name__icontains=search_name) &
- Q(email__icontains=search_email)
- )
- elif search_name is not None:
- authors = Author.objects.filter(full_name__icontains=search_name)
- elif search_email is not None:
- authors = Author.objects.filter(email__icontains=search_email)
- else:
- return ""
- if not authors.exists():
- return ""
- authors = authors.order_by('-full_name')
- result = []
- for author in authors:
- status = 'Banned' if author.is_banned else 'Not Banned'
- result.append(f"Author: {author.full_name}, email: {author.email}, status: {status}")
- return "\n".join(result)
- def get_top_publisher():
- top_publisher = Author.objects.annotate(num_of_articles=Count('article')).\
- order_by('-num_of_articles', 'email').first()
- if top_publisher and top_publisher.num_of_articles > 0:
- return f"Top Author: {top_publisher.full_name} with {top_publisher.num_of_articles} published articles."
- return ""
- def get_top_reviewer():
- top_reviewer = Author.objects.annotate(num_of_reviews=Count('review')).order_by('-num_of_reviews', 'email').first()
- if top_reviewer and top_reviewer.num_of_reviews > 0:
- return f"Top Reviewer: {top_reviewer.full_name} with {top_reviewer.num_of_reviews} published reviews."
- return ""
- def get_latest_article():
- latest_article = Article.objects.order_by('-published_on').first()
- if not latest_article:
- return ""
- authors = latest_article.authors.order_by('full_name')
- author_names = ", ".join([author.full_name for author in authors])
- num_reviews = Review.objects.filter(article=latest_article).count()
- avg_rating = Review.objects.filter(article=latest_article).aggregate(Avg('rating'))['rating__avg'] or 0
- return f"The latest article is: {latest_article.title}. Authors: {author_names}. " \
- f"Reviewed: {num_reviews} times. Average Rating: {avg_rating:.2f}."
- def get_top_rated_article():
- top_rated_article = Article.objects.annotate(avg_rating=Avg('review__rating')).\
- order_by('-avg_rating', 'title').first()
- if not top_rated_article or top_rated_article.avg_rating is None:
- return ""
- num_reviews = Review.objects.filter(article=top_rated_article).count()
- avg_rating = top_rated_article.avg_rating
- return f"The top-rated article is: {top_rated_article.title}, with an average rating of {avg_rating:.2f}, " \
- f"reviewed {num_reviews} times."
- def ban_author(email=None):
- if email is None:
- return "No authors banned."
- try:
- author = Author.objects.get(email=email)
- except Author.DoesNotExist:
- return "No authors banned."
- num_reviews = Review.objects.filter(author=author).count()
- Review.objects.filter(author=author).delete()
- author.is_banned = True
- author.save()
- return f"Author: {author.full_name} is banned! {num_reviews} reviews deleted."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement