Skip to content

Commit

Permalink
Merge branch 'bootstrap'
Browse files Browse the repository at this point in the history
  • Loading branch information
ar4s committed Mar 25, 2016
2 parents a601ed2 + ef3a77c commit 9cf3d0d
Show file tree
Hide file tree
Showing 20 changed files with 215 additions and 29 deletions.
18 changes: 13 additions & 5 deletions dedal/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
DedalListView,
DedalReadView,
DedalUpdateView,
ModelListView
)
from dedal.exceptions import ModelIsNotInRegisterError

Expand All @@ -18,7 +19,8 @@ class Dedal(object):
create_view_class = DedalCreateView
delete_view_class = DedalDeleteView

def __init__(self, model, actions):
def __init__(self, parent, model, actions):
self.parent = parent
self.actions = actions
self.model = model
self.add_views_to_class(actions)
Expand All @@ -32,7 +34,8 @@ def add_views_to_class(self, actions):
if get_class_view:
class_view = get_class_view()
setattr(self, action, class_view.as_view(
model=self.model, action_name=action
model=self.model, action_name=action,
site=self.parent
))

@property
Expand All @@ -56,7 +59,11 @@ def __init__(self):
self._register = {}

def register(self, model, actions):
self._register[model] = Dedal(model, actions)
self._register[model] = Dedal(self, model, actions)

@property
def registered_models(self):
return set(self._register.keys())

def unregister(self, model):
if not self.is_registered(model):
Expand All @@ -68,7 +75,9 @@ def is_registered(self, model):
return model in list(self._register)

def get_urls(self):
urlpatterns = []
urlpatterns = [
url(r'^$', ModelListView.as_view(site=self), name='dedal_main')
]
for model, dedal in self._register.items():
urlpatterns += [
url(r'^{}'.format(
Expand All @@ -81,5 +90,4 @@ def get_urls(self):
def urls(self):
return self.get_urls()


site = DedalSite()
15 changes: 13 additions & 2 deletions dedal/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title></title>
<title>{% block title %}{% endblock %}</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
</head>
<body>
{% block content %}{% endblock %}
{% block nav %}{% endblock %}
<div class="container">
{% block content %}{% endblock %}
</div>
<footer>
{% block footer %}{% endblock %}
</footer>
</body>
</html>
23 changes: 23 additions & 0 deletions dedal/templates/dedal/base.html
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
{% extends 'base.html' %}

{% load dedal_tags %}

{% block nav %}
<nav class="navbar navbar-inverse navbar">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="{% url 'dedal_main' %}">Django Dedal</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
{% for item in models %}
<li{% if model|repr == item|repr %} class="active"{% endif %}>
<a href="{% crud item 'list' %}">
{{ item|verbose_name_plural|title }}
</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</nav>
{% endblock %}
1 change: 1 addition & 0 deletions dedal/templates/dedal/generic_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

{% load i18n %}

{% block legend_title %}{% trans 'Add form' %}{% endblock %}
{% block button_label %}{% trans 'Create' %}{% endblock %}
3 changes: 2 additions & 1 deletion dedal/templates/dedal/generic_delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<form method="POST">
{% csrf_token %}
{% trans "Are you sure?" %}
<button>{% trans "Yes" %}</button> <a href="{% crud object 'read' %}">{% trans "No" %}</a>
<button class="btn btn-success">{% trans "Yes" %}</button>
<a class="btn btn-primary" href="javascript:history.back()">{% trans "No" %}</a>
</form>
{% endblock %}
11 changes: 6 additions & 5 deletions dedal/templates/dedal/generic_form.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{% extends 'dedal/base.html' %}

{% load i18n dedal_tags %}
{% load i18n dedal_tags bootstrap %}

{% block content %}
<form method="POST">
<form role="form" method="POST">
<legend>{% block legend_title %}{% endblock %}</legend>
{% csrf_token %}
{{ form }}
<button>{% block button_label %}{% endblock %}</button>
<a href="{% crud model 'list' %}">{% trans 'Cancel' %}</a>
{{ form|bootstrap }}
<button class="btn btn-success">{% block button_label %}{% endblock %}</button>
<a class="btn btn-danger" href="javascript:history.back()">{% trans 'Cancel' %}</a>
</form>
{% endblock %}
23 changes: 18 additions & 5 deletions dedal/templates/dedal/generic_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@
{% load i18n dedal_tags %}

{% block content %}
<ul>
<h2>
{{ model|verbose_name_plural|title }}
<a class="btn btn-success" href="{% crud model 'create' %}" title="{% blocktrans %}Create {{ verbose_name }}{% endblocktrans %}">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</a>
</h2>
<ul class="list-group">
{% for object in object_list %}
<li>
<li class="list-group-item">
<a href="{% crud object 'read' %}">{{ object }}</a>
<a href="{% crud object 'update' %}">{% trans 'Update' %}</a>
<a href="{% crud object 'delete' %}">{% trans 'Delete' %}</a>
<div class="pull-right">
<a class="btn btn-primary btn-xs" href="{% crud object 'update' %}">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
{% trans 'Update' %}
</a>
<a class="btn btn-danger btn-xs" href="{% crud object 'delete' %}">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
{% trans 'Delete' %}
</a>
</div>
</li>
{% empty %}
<p>{% trans 'Nothing to display.' %}</p>
{% endfor %}
<a href="{% crud model 'create' %}">{% blocktrans %}Create {{ verbose_name }}{% endblocktrans %}</a>
</ul>
{% endblock %}
23 changes: 23 additions & 0 deletions dedal/templates/dedal/generic_model_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends 'dedal/base.html' %}

{% load i18n dedal_tags %}

{% block content %}
<h2>
{% trans 'Model list' %}
</h2>
<ul class="list-group">
{% for model in models %}
<li class="list-group-item">
<a href="{% crud model 'list' %}">{{ model|verbose_name_plural|title }}</a>
<div class="pull-right">
<a class="btn btn-success btn-xs" href="{% crud model 'create' %}" title="{% blocktrans %}Create {{ verbose_name }}{% endblocktrans %}">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</a>
</div>
</li>
{% empty %}
<p>{% trans 'Nothing to display.' %}</p>
{% endfor %}
</ul>
{% endblock %}
19 changes: 15 additions & 4 deletions dedal/templates/dedal/generic_read.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
{% load i18n dedal_tags %}

{% block content %}
<a href="{% crud object 'list' %}">{% trans 'Back to list' %}</a>
{% for name, value in fields.items %}
{{ name }} {{ value }}
<h2>
{{ object }}
<span class="small">id: {{ object.pk }}</span>
<a class="btn btn-primary" href="{% crud object 'list' %}">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> {% trans 'Back to' %} {{ object|verbose_name_plural }}
</a>
</h2>
<dl>
{% for name, value in fields.items %}
<dt>{{ name }}</dt>
<dd>{{ value }}</dd>
{% endfor %}
<a href="{% crud object 'update' %}">{% trans "Update" %}</a> <a href="{% crud object 'delete' %}">{% trans "Delete" %}</a>
<a class="btn btn-primary" href="{% crud object 'update' %}">{% trans 'Update' %}</a>
<a class="btn btn-danger" href="{% crud object 'delete' %}">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> {% trans 'Delete' %}
</a>
{% endblock %}
1 change: 1 addition & 0 deletions dedal/templates/dedal/generic_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

{% load i18n %}

{% block legend_title %}{% trans 'Edit form' %}{% endblock %}
{% block button_label %}{% trans 'Save' %}{% endblock %}
9 changes: 9 additions & 0 deletions dedal/templatetags/dedal_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ def crud(obj, action):
if action in ACTIONS_REQUIRED_OBJ:
args = [obj.pk]
return reverse('{}_{}'.format(obj_name, action), args=args)


register.filter(
'verbose_name_plural', lambda x: x._meta.verbose_name_plural
)

register.filter(
'repr', lambda x: repr(x)
)
17 changes: 14 additions & 3 deletions dedal/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.db.models.fields import Field
from django.core.urlresolvers import reverse
from django.views.generic import (
CreateView,
DeleteView,
DetailView,
ListView,
UpdateView
UpdateView,
TemplateView
)

from django.forms.models import model_to_dict
Expand All @@ -13,6 +15,7 @@
class DedalBaseMixin(object):
action_name = None
model = None
site = None

@property
def model_name(self):
Expand All @@ -26,10 +29,14 @@ def get_template_names(self):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['model'] = self.model
context['verbose_name'] = self.model._meta.verbose_name
context['models'] = self.site.registered_models
return context


class ModelListView(DedalBaseMixin, TemplateView):
template_name = 'dedal/generic_model_list.html'


class DedalListView(DedalBaseMixin, ListView):
pass

Expand All @@ -45,7 +52,11 @@ class DedalModelFormMixin(object):
@property
def fields(self):
# TODO: blacklist, editable from field
return self.model._meta.get_all_field_names()
return [
field.name
for field in self.model._meta.get_fields()
if isinstance(field, Field)
]


class DedalUpdateView(
Expand Down
25 changes: 25 additions & 0 deletions example/blog/migrations/0002_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2016-03-25 19:16
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('author', models.CharField(max_length=25)),
('email', models.EmailField(max_length=254)),
('www', models.URLField()),
('body', models.TextField()),
],
),
]
25 changes: 25 additions & 0 deletions example/blog/migrations/0003_auto_20160325_1918.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2016-03-25 19:18
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0002_comment'),
]

operations = [
migrations.AddField(
model_name='post',
name='comments',
field=models.ManyToManyField(to='blog.Comment'),
),
migrations.AlterField(
model_name='comment',
name='www',
field=models.URLField(help_text='Enter address to your website.', verbose_name='Website'),
),
]
17 changes: 17 additions & 0 deletions example/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,20 @@
class Post(models.Model):
title = models.CharField(max_length=50)
body = models.TextField()
comments = models.ManyToManyField('Comment', blank=True)

def __str__(self):
return '{}'.format(self.title)


@crud
class Comment(models.Model):
author = models.CharField(max_length=25)
email = models.EmailField()
www = models.URLField(
verbose_name='Website', help_text='Enter address to your website.'
)
body = models.TextField()

def __str__(self):
return '{} - {}'.format(self.author, self.body)
6 changes: 6 additions & 0 deletions example/blog/templates/dedal/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'dedal/base.html' %}

{% block nav %}
{{ block.super }}
<a href="https://github.com/you"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/652c5b9acfaddf3a9c326fa6bde407b87f7be0f4/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"></a>
{% endblock %}
Binary file modified example/db.sqlite3
Binary file not shown.
3 changes: 2 additions & 1 deletion example/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dedal',
'blog',
'dedal',
'bootstrapform',
)

MIDDLEWARE_CLASSES = (
Expand Down
4 changes: 1 addition & 3 deletions example/example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin
from dedal.site import site as dedal_site

urlpatterns = [
url(r'^dedal/', include(dedal_site.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include(dedal_site.urls)),
]

0 comments on commit 9cf3d0d

Please sign in to comment.