AlexW
AlexW

Reputation: 2591

How to format data for a django template?

This is a sample of my data:

Network Policy
    Level 1: John Smith
    Start date: 27 April
    Start time: 8am
    end Date: 05 May
    end time: 8am
    Level 2: Bob Smith
    Start date: 27 April
    Start time: 8am
    end Date: 05 May
    end time: 8am
Server Policy
    Level 1: Jane Doe
    Start date: 23 April
    Start time: 8am
    end Date: 02 May
    end time: 8am
    Level 2: Greg Brad
    Start date: 23 April
    Start time: 8am
    end Date: 02 May
    end time: 8am

I need to arrange this data in such a format that I can use it as a template in django. I thought I needed nested dictionaries at first, but that doesn't work, as I need multiple values under the same Key.

I'm new to Python, so please correct the above if I'm wrong.

EDIT:

this is as far as ive got,

i have Dict > list , not sure how to get a dict into the list though that matches?

### Get all the Polices ###
dictContext = {}
for objPolicy in objPolicyData['escalation_policies']:
    strPolicyName = objPolicy['name']   
    if strPolicyName.lower().find('test') == -1:
        lstLevel = []
        dictContext[strPolicyName] = lstLevel
        for objOnCall in objPolicy['on_call']:
            dictUser = {}
            lstLevel.append(objOnCall['level'])
            dictUser['strStartDate'] =  getDate(objOnCall['start'])
            dictUser['strStartTime'] = getTime(objOnCall['start'])
            dictUser['strEndDate'] =  getDate(objOnCall['end'])
            dictUser['strEndTime'] = getTime(objOnCall['end'])
            objUser = objOnCall['user']
            dictUser['strUsername'] =  objUser['name']
            dictUser['strUserMobile'] = getUserMobile(objUser['id'])
            #lstLevel.append(dictUser)

result is

{
    u'DBA Escalation Policy': [1],
    u'Server Escalation Policy': [1],
    u'Network Team Escalation Policy': [1, 2],
    }

so my next step is to have the user details under each level

it needs to go

Policy1 > Level 1 > level 1 user details | Level 2 > Level 2 user details
Policy2 > Level 1 > level 1 user details 
Policy3 > Level 1 > level 1 user details | Level 2 > Level 2 user details | Level 3 > Level 3 user details

this is my goal

Thanks

Upvotes: 2

Views: 351

Answers (1)

SpoonMeiser
SpoonMeiser

Reputation: 20397

Using primitive types, your data probably translates to something like this:

policy = {
    "Network": [
        {
            "Name": "John Smith",
            "Start_date": "27 April",
            "Start_time": "8am",
            "end_Date": "05 May",
            "end_time": "8am"
        },
        {
            "Name": "Bob Smith",
            "Start_date": "27 April",
            "Start_time": "8am",
            "end_Date": "05 May",
            "end_time": "8am"
        },
    ]
    "Server": [
        # ...
    ]
}

In a django template then, you could refer to, for example:

{% policy.Network.0.name %}

(levels are 0-indexed, so use level - 1)

And you can also iterate over the levels in each policy.

To make this data structure from the data structure you already have (judging by your code example) you'd do something like:

policy = {}

# is there a better way here? Are we iterating a dict or a list?
for policy_data in objPolicyData['escalation_policies']:
    policy_name = policy_data["name"]

    # this is a better approach that the find method you employed
    if "test" in policy_name:
        continue

    def sort_key(user_data):
        return user_data["level"]

    # just using a list comprehension to map the objects from the current
    # form to your desired form, making sure we iterate over them in
    # level order
    policy[policy_name] = [
        {
            "name": user_data["name"]
            "start_date": getDate(user_data['start'])
            "start_time": getTime(user_data['start'])
            "end_date": getDate(user_data['end'])
            "end_time": getTime(user_data['end'])
        }
        for user_data in sorted(policy_data["on_call"], key=sort_key)
    ]

I'd probably consider having objects that represent your data, and providing an interface that works with django templates' limitations, rather than using primitives, because otherwise you end up with both your templates and your view code being tightly coupled to the structure of the data.

Upvotes: 1

Related Questions