This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
from bs4 import BeautifulSoup | |
def parse_page(page, session): | |
response = session.get(page) | |
soup = BeautifulSoup(response.text, 'html.parser') | |
title_text = soup.find('h1').text | |
return {'title': title_text} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import concurrent.futures | |
import requests | |
import threading | |
import time | |
from bs4 import BeautifulSoup | |
thread_storage = threading.local() | |
def get_session(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aiohttp | |
import asyncio | |
import time | |
from bs4 import BeautifulSoup | |
async def parse_page(page, session): | |
async with session.get(page) as response: | |
raw_text = await response.text() | |
soup = BeautifulSoup(raw_text, 'html.parser') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Book(models.Model): | |
title = models.CharField(max_length=140) | |
author = models.CharField(max_length=140) | |
year = models.IntegerField() | |
finished = models.BooleanField() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from book.models import Book | |
class BookSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = Book | |
fields = '__all__' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import viewsets | |
from rest_framework.decorators import action | |
from rest_framework.response import Response | |
from book.models import Book | |
from book.serializers import BookSerializer | |
class BookViewSet(viewsets.ModelViewSet): | |
queryset = Book.objects.all() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import routers | |
from book import views | |
router = routers.DefaultRouter() | |
router.register('', views.BookViewSet, basename='book') | |
urlpatterns = router.urls | |
print(urlpatterns) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from book.models import Book | |
def create_random_book(finished: bool = True) -> Book: | |
return Book.objects.create( | |
title=str(random.randint(1111, 9999)), | |
author=str(random.randint(1111, 9999)), | |
year=random.randint(1980, 2021), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.test import TestCase | |
from book.tests import helper | |
class BaseBookTest(TestCase): | |
def setUp(self) -> None: | |
self.client = self.client_class() | |
self.book_finished_list = helper.generate_random_books(10, True) | |
self.book_unfinished_list = helper.generate_random_books(10, False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from rest_framework.reverse import reverse | |
from book.models import Book | |
from book.tests.base import BaseBookTest | |
class BookAPITest(BaseBookTest): | |
def verify_book(self, book: Book, book_dict: dict): |
OlderNewer