import{_ as a,c as r,o as t,ah as i}from"./chunks/framework.j4Nev8bF.js";const u=JSON.parse('{"title":"Servers and more","description":"","frontmatter":{},"headers":[],"relativePath":"rebel_coding/step5.md","filePath":"rebel_coding/step5.md"}'),o={name:"rebel_coding/step5.md"};function s(n,e,l,d,h,p){return t(),r("div",null,[...e[0]||(e[0]=[i(`<h1 id="servers-and-more" tabindex="-1">Servers and more <a class="header-anchor" href="#servers-and-more" aria-label="Permalink to “Servers and more”">​</a></h1><hr><p>We introduced Databases in Step 4; now we&#39;re going to take the scrapers we&#39;ve built, and add them to an in-production Django server application!</p><h1 id="mvc-model-view-control" tabindex="-1">MVC - Model View Control <a class="header-anchor" href="#mvc-model-view-control" aria-label="Permalink to “MVC - Model View Control”">​</a></h1><hr><p><a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" target="_blank" rel="noreferrer">MVC</a> is a way to divide the components of a full-stack website into smaller parts; more technically known as a <em>software design pattern</em>.</p><p><strong>Models</strong> define the data that forms the software&#39;s content. Blog posts, images, etc; anything and everything that sits in a database is defined by the models.</p><p><strong>Views</strong> control the presentation of software&#39;s content; when a web-page is visited, views determine how the content is presented.</p><p><strong>Controllers</strong> do the heavy lifting between models and views, by retrieving the data and responsing to user input.</p><h3 id="how-does-django-differ" tabindex="-1">How does Django differ? <a class="header-anchor" href="#how-does-django-differ" aria-label="Permalink to “How does Django differ?”">​</a></h3><p>The server system that we focus on in Rebel Coding is Django with puts a bit of a twist on the ol&#39; MVC model; namely by switching out Views for Templates and referring to Controllers and Views ...</p><p>If it sounds confusing, you won&#39;t be the first to be befuddled; and it is easily comprehended once you start working with the toolset.</p><p>As a matter of fact, we don&#39;t even bother with the Template part in the remainder of this course, as we pass that task off to a JavaScript client instead.</p><h1 id="why-django" tabindex="-1">Why Django? <a class="header-anchor" href="#why-django" aria-label="Permalink to “Why Django?”">​</a></h1><hr><p>Because Django pre-packages as administrative interface, pure, plain and simple.</p><h3 id="models-py" tabindex="-1">Models.py <a class="header-anchor" href="#models-py" aria-label="Permalink to “Models.py”">​</a></h3><pre><code>class Post(models.Model):
    title = models.CharField(max_length=255)
    pub_date = models.DateTimeField(auto_now_add=False)
    content = models.TextField()
    slug = models.SlugField(unique=True, max_length=255)

    published = models.BooleanField(default=False)
#    category = models.ForeignKey(Category, blank=True, null=True, on_delete=&quot;PROTECT&quot;)
#    tags = models.ManyToManyField(Tag, blank=True)

    class Meta:
        ordering = [&#39;-pub_date&#39;]

    def __unicode__(self):
        return u&#39;%s&#39; % self.title

    def __str__(self):
        return u&#39;%s&#39; % self.title
</code></pre><h3 id="views-py" tabindex="-1">Views.py <a class="header-anchor" href="#views-py" aria-label="Permalink to “Views.py”">​</a></h3><pre><code>@permission_classes([])
@authentication_classes([])
class VideosAPIView(generics.ListAPIView):
    serializer_class = VideoSerializer

    def get_queryset(self):
        queryset = Video.objects.all()
        lang = self.request.query_params.get(&#39;q&#39;, None) 
        if lang == None:
            # lang = 0
            queryset = Video.objects.filter(published=True).all()
        else:
            queryset = Video.objects.filter(published=True, target_language=lang).all()
        return queryset
</code></pre><h3 id="urls-py" tabindex="-1">URLS.py <a class="header-anchor" href="#urls-py" aria-label="Permalink to “URLS.py”">​</a></h3><pre><code>urlpatterns = [
     url(r&#39;^api/searchvids$&#39;, SearchVideosAPIView.as_view(), name=&quot;search-vids&quot;),      
     url(r&#39;^api/imenu$&#39;, SocialAPIView.as_view(), name=&quot;social&quot;),   
     url(r&#39;^api/iposts$&#39;, PostsAPIView.as_view(), name=&quot;posts&quot;),
     url(r&#39;^api/ivids$&#39;, VideosAPIView.as_view(), name=&quot;videos&quot;),
     url(r&#39;^api/ivid/[-@\\w]+/$&#39;, VideoAPIView.as_view(), name=&quot;video&quot;),
     url(r&#39;^api/iverse$&#39;, VerseAPIView.as_view(), name=&quot;verse&quot;),     
     url(r&#39;^api/ividask$&#39;, RequestAPIView.as_view(), name=&quot;videoask&quot;),
     url(r&#39;^api/songsubmit$&#39;, UserAvatarUpload.as_view(), name=&quot;songsubmit&quot;),
     url(r&#39;^api/getidiokers$&#39;, GetIdiokersAPIView.as_view(), name=&quot;idiokers&quot;),
     url(r&#39;^api/getgenres$&#39;, GetGenreAPIView.as_view(), name=&quot;musicgenres&quot;),
]
</code></pre><h1 id="apis-the-django-rest-framework" tabindex="-1">APIS &amp; the Django REST Framework <a class="header-anchor" href="#apis-the-django-rest-framework" aria-label="Permalink to “APIS &amp; the Django REST Framework”">​</a></h1><hr><p>As mentioned earlier, we aren&#39;t bothering with Django Templates, rather all of our data will be consumed by a JavaScript client created using the VueJS framework.</p><p>Our Django server&#39;s duty is to serve API endpoints, and we do this using the <a href="https://www.django-rest-framework.org/" target="_blank" rel="noreferrer">Django REST Framework</a> .</p><p><strong>REST</strong> stands for <a href="https://en.wikipedia.org/wiki/Representational_state_transfer" target="_blank" rel="noreferrer">Representation State Transfer</a> ; and it is a style of software architecture to define how data is transferred between web services.</p><p>Other architectures include <a href="https://en.wikipedia.org/wiki/SOAP" target="_blank" rel="noreferrer">SOAP</a> and <a href="https://en.wikipedia.org/wiki/GraphQL" target="_blank" rel="noreferrer">GraphQL</a> .</p><h3 id="serializers-py" tabindex="-1">Serializers.py <a class="header-anchor" href="#serializers-py" aria-label="Permalink to “Serializers.py”">​</a></h3><p>RESTful frameworks operate by serializing, or conforming, data into a known structure and transferring this known structure between web services. And to do that we use what is called a serializer.</p><p>Just as our models define how the data is stored in our databases, serializers define the forme and shape of data that is transferred via API calls to our RESTful API endpoints.</p><pre><code>class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = &#39;__all__&#39;
#        fields = (&#39;id&#39;, &#39;name&#39;,)
</code></pre><h1 id="clone-sf-project" tabindex="-1">Clone SF Project <a class="header-anchor" href="#clone-sf-project" aria-label="Permalink to “Clone SF Project”">​</a></h1><hr><pre><code>git clone
git clone
</code></pre><h1 id="writing-your-first-web-scraper" tabindex="-1">Writing your first web-scraper <a class="header-anchor" href="#writing-your-first-web-scraper" aria-label="Permalink to “Writing your first web-scraper”">​</a></h1><hr><h1 id="attach-scraper-to-orm" tabindex="-1">Attach Scraper to ORM <a class="header-anchor" href="#attach-scraper-to-orm" aria-label="Permalink to “Attach Scraper to ORM”">​</a></h1><hr><p>Learn pupa etc …</p><pre><code>pupa init NewCity
</code></pre><h1 id="your-first-pull-request" tabindex="-1">Your First Pull Request <a class="header-anchor" href="#your-first-pull-request" aria-label="Permalink to “Your First Pull Request”">​</a></h1><hr><p>Fork the project</p><h1 id="exploring-the-sf-project" tabindex="-1">Exploring the SF Project <a class="header-anchor" href="#exploring-the-sf-project" aria-label="Permalink to “Exploring the SF Project”">​</a></h1><hr>`,46)])])}const f=a(o,[["render",s]]);export{u as __pageData,f as default};
