Пример #1
0
    def process_item(self, item, spider):
        """Save quotes in the database
        This method is called for every item pipeline component
        """
        session = self.Session()
        quote = Quote()
        author = Author()
        tag = Tag()
        author.name = item["author_name"]
        author.birthday = item["author_birthday"]
        author.bornlocation = item["author_bornlocation"]
        author.bio = item["author_bio"]
        quote.quote_content = item["quote_content"]

        # check whether the author exists
        exist_author = session.query(Author).filter_by(
            name=author.name).first()
        if exist_author is not None:  # the current author exists
            quote.author = exist_author
        else:
            quote.author = author

        # check whether the current quote has tags or not
        if "tags" in item:
            for tag_name in item["tags"]:
                tag = Tag(name=tag_name)
                # check whether the current tag already exists in the database
                exist_tag = session.query(Tag).filter_by(name=tag.name).first()
                if exist_tag is not None:  # the current tag exists
                    tag = exist_tag
                quote.tags.append(tag)

        try:
            session.add(quote)
            session.commit()

        except:
            session.rollback()
            raise

        finally:
            session.close()

        return item
Пример #2
0
    def handle(self, *args, **options):
        with open(options['json_file']) as json_file:
            data = json.load(json_file)
        for quote_data in data:
            author, _ = Author.objects.get_or_create(name=quote_data['author'])

            quote = Quote()
            quote.text = quote_data['text']
            quote.author = author
            quote.save()
        print('Done')
Пример #3
0
def quotesformview(request):
    if request.method == 'POST':
        if request.POST.get('title') and request.POST.get(
                'person') and request.POST.get('quote'):
            quote = Quote()
            #quote_cate = QuoteCategory()
            quote.title = request.POST.get('title')
            quote.author = request.POST.get('person')
            quote.quote = request.POST.get('quote')
            quote.owner = request.POST.get('owner')
            quote.save()
            return redirect('quotes')
    else:
        return render(request, 'quotesform.html')
Пример #4
0
def home(request):
    """
    Displays list of generated quotes from quotesondesign.
    """
    if request.method == "GET":

        posts = request.GET.get('posts', 4)
        page = request.GET.get('page', 1)

        # response = requests.get("https://quotesondesign.com/wp-json/wp/v2/posts?filter[orderby]=rand&filter[posts_per_page]=30")
        response = requests.get(
            "https://quotesondesign.com/wp-json/wp/v2/posts/?orderby=rand&filter[posts_per_page]=30"
        )

        data = response.json()

        quotes = []

        for dict_item in data:

            quote = Quote()

            quote.content = list(
                map(lambda d: parse_content(d), [
                    value['rendered']
                    for key, value in dict_item.items() if key == "content"
                ]))[0]  # temporary update after API v5.0

            quote.author = dict_item['title'][
                'rendered']  # temporary update after API v5.0

            check = Quote.objects.filter(content=quote.content).all(
            )  # returns Queryset if quote content exists

            if not check:
                quote.save()
            else:
                quote.id = check.values("id")[0][
                    'id']  # If in database, store existing quote id into object sent for template rendering

                q = Quote.objects.get(
                    id=quote.id)  # fetches quote from database
                quote.no_user_likes = q.like_set.all().count(
                )  # generate number of user likes for particular quote
                quote.no_user_favourites = q.favourite_set.all().count(
                )  # generate number of user likes for particular quote

                quote.check_liked = check.values("check_liked")[0][
                    "check_liked"]  # check if quote has been liked by logged in user
                quote.check_favourited = check.values("check_favourited")[0][
                    "check_favourited"]  # check if quote has been favourited by logged in user

            quotes.append(quote)

        # Paginate quotes
        quotes = quotes_paginator(quotes, posts, page)

        return render(
            request, 'quotes/home.html', {
                'quotes': quotes,
                'posts': posts,
                'page': page,
                'max_posts': list(range(3, 6))
            })

    elif request.method == "POST":
        # URL queries are always fetched via GET, not POST; request.POST.get() is only for retrieving from request.body
        posts = request.GET.get('posts', 4)
        page = request.GET.get('page', 1)

        if request.user is not AnonymousUser and isinstance(
                request.POST.get('submit_user_like'), str):

            quote_id = request.POST.get("submit_user_like")
            Like.objects.get_or_create(
                user=request.user, quote_id=quote_id
            )  # Creates Like entry in table quotes_like. Outputs (Like object, boolean on whether created)
            quote = Quote.objects.get(id=quote_id)

            if (quote.check_liked == True):
                quote.check_liked = False
                quote.save()
                Like.objects.filter(quote=quote_id).delete()
                messages.success(request, "Unliked!")
            else:
                quote.check_liked = True
                quote.save()
                messages.success(request, "Liked!")

        elif request.user is not AnonymousUser and isinstance(
                request.POST.get('submit_user_favourite'), str):

            quote_id = request.POST.get("submit_user_favourite")
            Favourite.objects.get_or_create(
                user=request.user,
                quote_id=request.POST.get('submit_user_favourite'))
            quote = Quote.objects.get(id=quote_id)

            if (quote.check_favourited == True):
                quote.check_favourited = False
                quote.save()
                Favourite.objects.filter(quote=quote_id).delete()
                messages.success(request, "Removed from favourites!")
            else:
                quote.check_favourited = True
                quote.save()
                messages.success(request, "Favourited!")

        # Build redirect string back to 'quotes-home' with 'posts' and 'page' url query
        base_url = reverse('quotes-home')
        query_string = urlencode({'posts': posts, 'page': page})
        url = '{}?{}'.format(base_url, query_string)

        return redirect(url)