Skip to content

Instantly share code, notes, and snippets.

@rg3915
Last active June 28, 2023 02:52
Show Gist options
  • Save rg3915/9694962750d735177e8d0c43e8441894 to your computer and use it in GitHub Desktop.
Save rg3915/9694962750d735177e8d0c43e8441894 to your computer and use it in GitHub Desktop.
group table permissions product groups actions
{% extends "base.html" %}
{% block content %}
<table class="table">
<thead>
<tr>
<th></th>
{% for action in actions %}
<th>{{ action }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for group in groups %}
<tr>
<td>{{ group }}</td>
{% for action in actions %}
<td>{{ group }}, {{ action }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
<table class="table">
<thead>
<tr>
<th></th>
{% for action in actions %}
<th>{{ action }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for permission in permissions %}
<tr>
<td>{{ permission.0 }}</td>
<td>{{ permission.1.0 }}</td>
<td>{{ permission.1.1 }}</td>
<td>{{ permission.1.2 }}</td>
<td>{{ permission.1.3 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
from itertools import groupby, product
from operator import itemgetter
def index(request):
template_name = 'index.html'
groups = ('gerente', 'vendedor', 'comprador')
actions = ('C', 'R', 'U', 'D')
context = {
'groups': groups,
'actions': actions,
}
return render(request, template_name, context)
def index(request):
# Segundo exemplo, usando groupby
template_name = 'index.html'
groups = ('gerente', 'vendedor', 'comprador')
actions = ('C', 'R', 'U', 'D')
products = list(product(groups, actions))
products.sort(key=itemgetter(0))
permissions = []
for key, group in groupby(products, itemgetter(0)):
permissions.append((key, list(group)))
context = {
'groups': groups,
'actions': actions,
'permissions': permissions,
}
return render(request, template_name, context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment