iterations of a list

26 views
Skip to first unread message

shul...@gmail.com

unread,
May 13, 2016, 9:35:14 PM5/13/16
to Django users
I'm basically trying to get employees of a manger using ldap. The error I get is TypeError at /aboutus-test/ cannot concatenate 'str' and 'list' objects. I think the issue is it's taking the list of employees and trying to put them all into a function rather than one by one. But I am not sure how to solve the issue, hence being here.

python: 2.7
django: 1.8

sample of my ldap script:

def getFTEs(manager):
conn = ldap.initialize("ldap://ourldapserver:389/")
results = conn.search_s("o=domain.com", ldap.SCOPE_SUBTREE, "uid=" + manager, attrlist=['employeetype'])
if len(results) >= 1:
Info = results[0][1]
directsInfo = str(Info)
directs = re.findall('\((.*?)\)', directsInfo)
if directs != []:
E = []
for direct in directs:
conn = ldap.initialize("ldap://ourldapserver:389/")
results = conn.search_s("o=domain.com", ldap.SCOPE_SUBTREE, "uid=" + direct, attrlist=['employeetype'])
directsinfo = results[0][1]
code = directsinfo['employeetype'][0]
if code == "F":
E.append(direct)
return E
else:
return 'no directs'

ftes = getFTEs(manager)

def names(ftes):
conn = ldap.initialize("ldap://ourldapserver:389/")
results = conn.search_s("o=domain.com", ldap.SCOPE_SUBTREE, "uid=" + ftes, attrlist=['gecos'])
name2 = results[0][1]
gecos = name2['gecos'][0]
return gecos

I have one of these for all the fields I want.

views.py:

def aboutus(request):
u = request.user.get_username()
username = u.split("@")[0]
ftes = getFTEs("managername")
name = names(ftes)
title = titles(ftes)
bldg = buildings(ftes)
email = emails(ftes)
context = RequestContext(request, {
'username': username,
'ftes': ftes,
'name': name,
'title': title,
'bldg': bldg,
'email': email
})
return render(request, 'about-test.html', context)

in the template:

{% for item in ftes %}


{{ item.name }}, {{ item.title }}, {{ item.bldg }}, {{ item.email }}<br>
{% endfor %}

James Schneider

unread,
May 14, 2016, 4:00:22 AM5/14/16
to django...@googlegroups.com


On May 13, 2016 6:34 PM, <shul...@gmail.com> wrote:
>
> I'm basically trying to get employees of a manger using ldap. The error I get is TypeError at /aboutus-test/ cannot concatenate 'str' and 'list' objects. I think the issue is it's taking the list of employees and trying to put them all into a function rather than one by one. But I am not sure how to solve the issue, hence being here.
>

Can you post the entire traceback? Without it, we don't know which line is causing the error.

-James

Shu Latif

unread,
May 14, 2016, 4:19:28 PM5/14/16
to Django users
It's this line in names(ftes)

    results = conn.search_s("o=domain.com", ldap.SCOPE_SUBTREE, "uid=" + ftes, attrlist=['gecos'])



James Schneider

unread,
May 14, 2016, 4:49:39 PM5/14/16
to django...@googlegroups.com


On May 14, 2016 1:19 PM, "Shu Latif" <shul...@gmail.com> wrote:
>
> It's this line in names(ftes)
>
>     results = conn.search_s("o=domain.com", ldap.SCOPE_SUBTREE, "uid=" + ftes, attrlist=['gecos'])
>

getFTE() can return with a string or with a list. You are using that value for ftes when calling names(ftes).

The line for your LDAP search is only expecting a string as a value for ftes, so it will bomb every time getFTE() returns a list.

-James

Reply all
Reply to author
Forward
0 new messages