https://github.com/klashxx/PyConES2017
SECRET_KEY=#^p^js0g91jhtarws6bp7s@r&988%%n_ok6#b)q=3s1v8-(si$
from decouple import config
SECRET_KEY = config('SECRET_KEY')
DB_URL=postgresql://postgres:postgres@postgres:5432/postgres
DATABASES = {
'default': dj_database_url.config(default=config('DB_URL')),
}
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
polls/
__init__.py
admin.py
migrations/
__init__.py
0001_initial.py
models.py
static/
polls/
images/
background.gif
style.css
templates/
polls/
detail.html
index.html
results.html
tests.py
urls.py
views.py
templates/
admin/
base_site.html
sysgate/
manage.py
sysgate/
__init__.py
settings.py
urls.py
wsgi.py
account/
__init__.py
…
metrics/
__init__.py
…
templates/
account/
login.html
metrics/
home.html
sysgate/
manage.py
sysgate/
__init__.py
settings.py
urls.py
wsgi.py
routers.py
account/
__init__.py
...
apps/
__init__.py
core/
templatetags/
__init__.py
core_tags.py
templates/
core/
home.html
...
__init__.py
...
metrics/
templates/
metrics/
home.html
...
__init__.py
...
manager/
templates/
manager/
home.html
...
__init__.py
...
static/
...
fixtures/
...
templates/
account/
login.html
...
base.html
urlpatterns = [
url(r'^', include('apps.core.urls', namespace='core')),
url(r'^account/', include('account.urls'), name='Autenticación'),
url(r'^metrics/', include('apps.metrics.urls'), name='Métricas'),
url(r'^manager/', include('apps.manager.urls'), name='Manager'),
url(r'^admin/', admin.site.urls),
]
urlpatterns = [
url(r'^$', views.Home.as_view(), name='home'),
]
Metrics App
Bienvenid@ {{ request.user }}
Disponibles:
{% for metrica in metricas %}
{{ metrica.nombre }}
{{ metrica.get_tipo_display }}
{% endfor %}
class Home(View):
def get(self, request, *args, **kwargs):
metricas = Metrica.objects.all().order_by('tipo')
return render(
request,
'metrics/home.html',
context={'metricas': metricas})
{% for metrica in metricas %}
{{ metrica.nombre }}
{{ metrica.get_tipo_display }}
{% endfor %}
class SerializerMetricas(serializers.ModelSerializer):
class Meta:
model = Metrica
fields = '__all__'
class MetricasViewSet(viewsets.ModelViewSet):
serializer_class = SerializerMetricas
http_method_names = ['get']
filter_backends = (OrderingFilter, SearchFilter,)
search_fields = ('tipo',)
def get_queryset(self):
queryset = Metrica.objects.all().order_by('tipo')
tipo = self.request.query_params.get('tipo', None)
if tipo is not None:
queryset = queryset.filter(tipo=tipo)
return queryset
router = routers.SimpleRouter()
router.register(r'metricas', views.MetricasViewSet, 'metricas')
app_name = 'metrics'
urlpatterns = [
url(r'^$', views.Home.as_view(), name='home'),
url(r'^api/v1/', include(router.urls)),
]
http://0.0.0.0/metrics/api/v1/metricas/
{
"nombre": "USERS",
"long_tipo": "Custom",
"tipo": "c",
"alta": "2017-08-13T10:21:19.015000Z",
"descripcion": "Return users ..."
},
{
"nombre": "READ",
"long_tipo": "Disk",
"tipo": "d",
"alta": "2017-08-13T10:15:09.516000Z",
"descripcion": "Number of reads"
}
+-- account
+-- apps
¦ +-- core
¦ ¦ +-- templates
¦ ¦ ¦ +-- core
¦ ¦ ¦ +-- home.html
¦ ¦ +-- templatetags
¦ ¦ ¦ +-- core_tags.py
¦ +-- metrics
¦ +-- templates
¦ ¦ +-- metrics
¦ ¦ +-- home.html
+-- templates
¦ +-- account
¦ ¦ +-- login.html
¦ +-- base.html
from django import template
register = template.Library()
@register.filter('en_grupo')
def en_grupo(user, group_name):
groups = user.groups.all().values_list('name', flat=True)
return True if group_name in groups else False
{% extends 'base.html' %}
{% load core_tags %}
{% block content %}
{% if request.user|en_grupo:"pycones" or user.is_superuser %}
Metrics
Acceder
{% endif %}
{% if not user.is_authenticated %}
Longin
{% endif %}
{% endblock %}
var v = new Vue({
delimiters: ['[[', ']]'],
el: '#container',
data: {
metricas: []
},
mounted: function(){
$.get('/metrics/api/v1/metricas/', function(data){
v.metricas = data;
})}
})
[[ metrica.nombre ]]
[[ metrica.long_tipo ]]
@method_decorator(login_required, name='dispatch')
class Home(UserPassesTestMixin, View):
def test_func(self):
return super_o_esta_en_grupo(self.request.user,
grupo='pycones')
def get(self, request, *args, **kwargs):
return render(request, 'metrics/home.html')
def super_o_esta_en_grupo(user, grupo):
if user.is_superuser:
return True
if user.groups.filter(name=grupo).exists():
return True
return False
from rest_framework.permissions import IsAuthenticated
class MetricasViewSet(viewsets.ModelViewSet):
grupos_autorizados = settings.GR_AUTH_METRICS
permission_classes = (IsAuthenticated, EsSuperOTienePermisosPorGrupo,)
class EsSuperOTienePermisosPorGrupo(BasePermission):
def has_permission(self, request, view):
gr_auth_map = getattr(view, 'grupos_autorizados', {})
gr_auth = gr_auth_map.get(request.method, [])
return any([super_o_esta_en_grupo(request.user, grupo)
for grupo in gr_auth])