GithubHelp home page GithubHelp logo

sachnaror / api-for-note-taking-web-app Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 594 KB

REST-ful API with the CRUD endpoints, to create, read, update, and delete notes.

Python 46.59% HTML 5.33% JavaScript 45.86% SCSS 2.22%

api-for-note-taking-web-app's Introduction

Rest API for a Google Keep-like note-taking web app

(along with Python Webpack Boilerplate).


If we do not need React or Vue, we can still use Webpack to help us compile ES6, SCSS, and do many other things (Many people do not know that!)

With a proper config, Webpack can save time and let us quickly build modern web applications.

Django lets us more or less create them all in one fell swoop.

During API-ification, i used Authorization class with TastyPie is, in their words, great for development — but not suitable for actual deployment. The magic of TastyPie is that PUT, DELETE is already done. Try updating or deleting my first note by ending PUT or DELETE to http://localhost:8000/api/note/1/. It just works! That means, just like that: I created a working RESTful API.

Lessons Learned

Limiting Fields : If I want to only send specific information about a resource, I can limit fields like so:

from tastypie.resources import ModelResource
from api.models import Note
from tastypie.authorization import Authorization
class NoteResource(ModelResource):
    class Meta:
        queryset = Note.objects.all()
        resource_name = 'note'
        authorization = Authorization()
        fields = ['title', 'body']

Make sure to send the request to http://localhost:8000/api/note/, not http://localhost:8000/api/note. That trailing slash is important, since otherwise Django has to redirect me, losing the POST data.

Send that request and… it fails. We get back a 401, AKA Unauthorized. Yay!

TastyPie is protective of its models out of the box, and only allows reading, not modifying, the data. It’s an easy fix, though — import their basic Authorization class, and add it to our resource.

api/resources.py

from tastypie.resources import ModelResource
from api.models import Note
from tastypie.authorization import Authorization
class NoteResource(ModelResource):
    class Meta:
        queryset = Note.objects.all()
        resource_name = 'note'
        authorization = Authorization()

Now it works! Try the request, and we get back 201, AKA success! Yay!


For Webpack

Available Scripts

In the project directory, you can run:

npm run start

npm run start will launch a server process, which makes live reloading possible.

If you change JS or SCSS files, the web page would auto refresh after the change. Now the server is working on port 9091 by default, but you can change it in webpack/webpack.config.dev.js

npm run watch

run webpack in watch mode.

npm run build

Webpack would focus on minified bundles, lighter weight source maps, and optimized assets to improve load time.

image

api-for-note-taking-web-app's People

Contributors

sachnaror avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.