Last active
January 14, 2021 12:30
-
-
Save tarsil/df06cc698bb601bc564f0e8b2d7eb17a to your computer and use it in GitHub Desktop.
Generics for Flask class based views that aims to replicate the Django Views
This file contains 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
# Copyright (c) 2020 Tiago Silva <https://github.com/tarsil> | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
from flask import abort | |
from flask import render_template | |
from flask import request | |
from flask.views import View | |
class ContextMixin: | |
""" | |
A default context mixin that passes the keyword arguments received by | |
get_context_data() as the template context. | |
""" | |
extra_context = None | |
def get_context_data(self, **kwargs): | |
kwargs.setdefault('view', self) | |
if self.extra_context is not None: | |
kwargs.update(self.extra_context) | |
return kwargs | |
class BaseView(ContextMixin, View): | |
""" | |
Default base view for the use of the generics | |
""" | |
template_name = None | |
allowed_methods = ["get", "post", "head", "options", "delete", "put", "trace", "patch"] | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.allowed_methods = [m.lower() for m in self.allowed_methods] | |
def get_template_name(self): | |
return self.template_name | |
def render_template(self, **context): | |
return render_template(self.get_template_name(), **context) | |
def render(self, *args, **kwargs): | |
"""Subclasses have to override this method to implement the | |
actual view function code. This method is called with all | |
the arguments from the URL rule. | |
""" | |
context = self.get_context_data(**kwargs) | |
return self.render_template(**context) | |
def dispatch_request(self, *args, **kwargs): | |
method = getattr(self, request.method.lower(), None) | |
# WE DONT WANT TO HANDLE WITH HEAD, SO WE RETRY WITH GET | |
if method is None and request.method == "HEAD": | |
method = getattr(self, "get", None) | |
if not method: | |
# RENDERS THE DEFAULT VIEW | |
return self.render(**kwargs) | |
if method and (request.method.lower() not in self.allowed_methods): | |
abort(403, "Not allowed method %s" % request.method.upper()) | |
return method(*args, **kwargs) | |
def dispatch(self, *args, **kwargs): | |
return self.dispatch_request(*args, **kwargs) | |
class ListView(BaseView): | |
""" | |
View representing a list of objects retrieved | |
Renders a template and pass a list of objects | |
Usage: | |
`class MyListView(ListView): | |
template_name = 'my_template.html' | |
def get_queryset(self): | |
return Users.query.all() | |
def get_context_data(**kwargs): | |
context = super().get_context_data(**kwargs) | |
return context | |
` | |
""" | |
def get_queryset(self): | |
raise NotImplementedError() | |
def dispatch_request(self, **kwargs): | |
context = self.get_context_data(**kwargs) | |
context.update({ | |
'objects': self.get_queryset() | |
}) | |
return self.render_template(**context) | |
class TemplateView(BaseView): | |
""" | |
This replaces the main View from flask by using something more user friendly and visually | |
more intuitive. | |
Renders a template | |
Usage: | |
`class MyViewView(TemplateView): | |
template_name = 'my_template.html' | |
def get_context_data(**kwargs): | |
context = super().get_context_data(**kwargs) | |
return context | |
` | |
""" | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment