Skip to content

Instantly share code, notes, and snippets.

View Joel-hanson's full-sized avatar
:octocat:
Looking for opportunities

Joel Hanson Joel-hanson

:octocat:
Looking for opportunities
View GitHub Profile
@Joel-hanson
Joel-hanson / gulpfile.js
Last active September 13, 2018 03:30
A gulp file for my dist build
var htmlmin = require('gulp-htmlmin');
var cleanCSS = require('gulp-clean-css');
var minify = require('gulp-minify');
const gulp = require('gulp'),
del = require('del'),
runSequence = require('run-sequence'),
src = `${__dirname}/.`,
dist = `${__dirname}/public`;
workbox = require('workbox-build');
imagemin = require('gulp-imagemin');
stages:
- deploy
deploy_prod:
stage: deploy
script:
- rm -r Web/public
- cd Web
- npm install
- gulp build && gulp generate-service-worker && gulp minify_html && gulp minify-css && gulp minify_js
This file was created because you tired to download from a localhost in docker.
@Joel-hanson
Joel-hanson / serializers.py
Created June 2, 2019 12:27
Advanced serializer usage - Dynamically modifying fields
from rest_framework.serializers import ModelSerializer, SerializerMethodField
from .models import Book, Publisher, Author
class PublisherModelSerializer(ModelSerializer):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed.
"""
def __init__(self, *args, **kwargs):
@Joel-hanson
Joel-hanson / models.py
Last active June 3, 2019 04:13
The example we are using to demonstrate dynamically modifying fields will have a Book model which has Publisher and Author as foreign and many to many relations.
from django.db import models
# Create your models here.
class Publisher(models.Model):
name = models.CharField(max_length=30, blank=False)
address = models.CharField(max_length=50, blank=False)
city = models.CharField(max_length=60, blank=False)
state_province = models.CharField(max_length=30, blank=False)
country = models.CharField(max_length=50, blank=False)
@Joel-hanson
Joel-hanson / views.py
Created June 3, 2019 04:16
We will be using the ModelViewSet and ModelSerializer to make the API for our Book model.
from rest_framework.viewsets import ModelViewSet
from rest_framework.authentication import SessionAuthentication
from .serializers import BookModelSerializer
from .models import Book
# Create your views here.
class BookModelViewSet(ModelViewSet):
"""
A simple ViewSet for viewing and editing books.
@Joel-hanson
Joel-hanson / urls.py
Created June 3, 2019 04:17
In this example, we are using a DefaultRouter which adds support for automatic URL routing to Django, and provides you with a simple, quick and consistent way of wiring your view logic to a set of URLs.
from rest_framework.routers import DefaultRouter
from .views import BookModelViewSet
router = DefaultRouter(trailing_slash=False)
router.register(r'book', BookModelViewSet, base_name='book')
urlpatterns = router.urls
@Joel-hanson
Joel-hanson / serializers.py
Created June 3, 2019 04:20
The Nested serializers fields can also be a dynamically modifying fields by using the SerializerMethodField . TheSerializerMethodField can be used to pass the context which contains the requestof the parent serializer, so that we can pass the fields required by us to the nested serializer.
class BookModelSerializer(ModelSerializer):
authors = SerializerMethodField("get_author_serializer")
publisher = SerializerMethodField("get_publisher_serializer")
class Meta:
model = Book
fields = '__all__'
def get_author_serializer(self, obj):
request = self.context.get('request')
@Joel-hanson
Joel-hanson / views.py
Last active July 31, 2020 10:33
views for file upload for django rest framework
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from .serializers import UploadSerializer
# ViewSets define the view behavior.
class UploadViewSet(ViewSet):
serializer_class = UploadSerializer
def list(self, request):
return Response("GET API")
@Joel-hanson
Joel-hanson / serializers.py
Created July 31, 2020 10:33
serializer for file upload in django rest framework
from rest_framework.serializers import Serializer, FileField
# Serializers define the API representation.
class UploadSerializer(Serializer):
file_uploaded = FileField()
class Meta:
fields = ['file_uploaded']