Commit bd2cb60b by Bence Dányi

firewall_gui: Some comments

parent dd20a906
...@@ -7,12 +7,14 @@ from firewall.fw import * ...@@ -7,12 +7,14 @@ from firewall.fw import *
from firewall.models import * from firewall.models import *
def req_staff(user): def req_staff(user):
''' decorator function for user permission checking '''
return user.is_staff return user.is_staff
def index(request): def index(request):
return render(request, 'firewall/index.html') return render(request, 'firewall/index.html')
def map_rule_target(rule): def map_rule_target(rule):
''' get the actual target from rule field (vlan|vlangroup|host|hostgroup|firewall) '''
return { return {
'name': rule.vlan.name, 'name': rule.vlan.name,
'id': rule.vlan.id, 'id': rule.vlan.id,
...@@ -36,37 +38,46 @@ def map_rule_target(rule): ...@@ -36,37 +38,46 @@ def map_rule_target(rule):
} }
def json_attr(entity, attr): def json_attr(entity, attr):
''' jsonify the `attr` attribute of `entity` '''
# an objects name usually is in the `name` attribute, but not always, so put here the exceptions
common_names = { common_names = {
'host': 'hostname', 'host': 'hostname',
'owner': 'username', 'owner': 'username',}
}
try: try:
return { return {
# if `attr` is an entity, parse its name&id
'ForeignKey': lambda entity: { 'ForeignKey': lambda entity: {
'id': entity.id, 'id': entity.id,
'name': getattr(entity, common_names[attr] if attr in common_names.keys() else 'name') 'name': getattr(entity, common_names[attr] if attr in common_names.keys() else 'name')
} if entity else None, } if entity else None,
# if `attr` is a date, format it with isoformat
'DateTimeField': lambda entity: entity.isoformat(), 'DateTimeField': lambda entity: entity.isoformat(),
# if `attr` is a Crazy ManyToMany field, fetch all objects, and get their name&id
'ManyToManyField': lambda field: [{ 'ManyToManyField': lambda field: [{
'id': entity.id, 'id': entity.id,
'name': getattr(entity, common_names[attr] if attr in common_names.keys() else 'name') 'name': getattr(entity, common_names[attr] if attr in common_names.keys() else 'name')
} for entity in field.all()] } for entity in field.all()]
}[entity._meta.get_field_by_name(attr)[0].__class__.__name__](getattr(entity, attr)) }[entity._meta.get_field_by_name(attr)[0].__class__.__name__](getattr(entity, attr))
except Exception as e: except Exception as e:
return getattr(entity, attr) # if `attr` is something else, we hope it can be converted to JSON
return getattr(entity, attr) return getattr(entity, attr)
def make_entity_lister(entity_type, mapping): def make_entity_lister(entity_type, mapping):
''' makes a function that lists the given entities '''
def jsonify(entity): def jsonify(entity):
''' jsonify one entity '''
result = {} result = {}
for attr in mapping: for attr in mapping:
# if `attr` is a tuple, the first element is the key in the JSON, the second is a lambda function that calculates the value
if type(attr) is tuple: if type(attr) is tuple:
result[attr[0]] = attr[1](entity) result[attr[0]] = attr[1](entity)
else: else:
# if `attr` is just a string, the try to jsonify it
result[attr] = json_attr(entity, attr) result[attr] = json_attr(entity, attr)
return result return result
def entity_lister(request): def entity_lister(request):
''' jsonify all objects of the given model type '''
return [jsonify(entity) for entity in entity_type.objects.all()] return [jsonify(entity) for entity in entity_type.objects.all()]
return entity_lister return entity_lister
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment