Get custom attribute from python ldap query - python

i succesfully retrive objects that i need from a Python ldap query, the result is like this
[('uid=xxxxxx,ou=People,dc=xxxxxxx,dc=eu', {'departmentcode': ['xxxxx']})], [('uid=xxxxxx,ou=People,dc=xxxxxxx,dc=eu', {'departmentcode': ['xxxxx']})]
But from this result, i need just departmentcode. So, How to acess departmentcode value?

Try:
data = [('uid=xxxxxx,ou=People,dc=xxxxxxx,dc=eu', {'departmentcode': ['xxxxx']})], [('uid=xxxxxx,ou=People,dc=xxxxxxx,dc=eu', {'departmentcode': ['xxxxx']})]
departmentcode = []
for elem in data:
departmentcode.append(elem[0][1]['departmentcode'])

Related

Initiating a GET request to Django web api and send a list of ids as an array

I'm trying to call a rest API in django and send a list of Ids as a paremeter (I can't use query string).
This works if I send only one, but how to specify it for an array of integers:
path('details/<int:id>/', views.get_details),
def get_details(request, id=0)
What if I have a method that will take a list of Ids:
def get_data(request, ids=[])
How should the url be defined so that the ids array is populated?
You can parse ids list manually like this
path('details/<ids>/', views.get_details),
def parse_ids(ids: str):
result = []
for id in ids.split(','):
try:
result.append(int(id))
except ValueError:
pass
return result
def get_details(request, ids):
id_list = parse_ids(ids)
# do stuff
...

How to pass variable in find() method in mongo db using python

TPID=[318,205,2624,2635]
Tid= len(TPID)
try:
mclient = MongoClient(host="tgl-mongodb22.rctanalytics.com", port=27017)
Db = mclient['sitereft4']
Db.authenticate('st_sitereference', 'rlQ2YnPKNlS0')
coll = Db['shopper_journey_sitedata']
for i in range(Tid):
data = coll.find({"third_party_site_id":318})
for datas in data:
None
print(datas["st_site_id"])
In place of 318 i need to pass the variable "Tid" so that it should run for all values.
how to do it ?
i tried below one it didn't worked:
data = coll.find({"third_party_site_id":Tid[i]})
If your goal is to find each site id in TPID=[318,205,2624,2635]
Structure the for loop logic as:
for i in TPID:
data = coll.find({"third_party_site_id":i})
print(data)

I have an Error with python flask cause of an API result (probably cause of my list) and my Database

I use flask, an api and SQLAlchemy with SQLite.
I begin in python and flask and i have problem with the list.
My application work, now i try a news functions.
I need to know if my json informations are in my db.
The function find_current_project_team() get information in the API.
def find_current_project_team():
headers = {"Authorization" : "bearer "+session['token_info']['access_token']}
user = requests.get("https://my.api.com/users/xxxx/", headers = headers)
user = user.json()
ids = [x['id'] for x in user]
return(ids)
I use ids = [x['id'] for x in user] (is the same that) :
ids = []
for x in user:
ids.append(x['id'])
To get ids information. Ids information are id in the api, and i need it.
I have this result :
[2766233, 2766237, 2766256]
I want to check the values ONE by One in my database.
If the values doesn't exist, i want to add it.
If one or all values exists, I want to check and return "impossible sorry, the ids already exists".
For that I write a new function:
def test():
test = find_current_project_team()
for find_team in test:
find_team_db = User.query.filter_by(
login=session['login'], project_session=test
).first()
I have absolutely no idea to how check values one by one.
If someone can help me, thanks you :)
Actually I have this error :
sqlalchemy.exc.InterfaceError: (InterfaceError) Error binding
parameter 1 - probably unsupported type. 'SELECT user.id AS user_id,
user.login AS user_login, user.project_session AS user_project_session
\nFROM user \nWHERE user.login = ? AND user.project_session = ?\n
LIMIT ? OFFSET ?' ('my_tab_login', [2766233, 2766237, 2766256], 1, 0)
It looks to me like you are passing the list directly into the database query:
def test():
test = find_current_project_team()
for find_team in test:
find_team_db = User.query.filter_by(login=session['login'], project_session=test).first()
Instead, you should pass in the ID only:
def test():
test = find_current_project_team()
for find_team in test:
find_team_db = User.query.filter_by(login=session['login'], project_session=find_team).first()
Asides that, I think you can do better with the naming conventions though:
def test():
project_teams = find_current_project_team()
for project_team in project_teams:
project_team_result = User.query.filter_by(login=session['login'], project_session=project_team).first()
All works thanks
My code :
project_teams = find_current_project_team()
for project_team in project_teams:
project_team_result = User.query.filter_by(project_session=project_team).first()
print(project_team_result)
if project_team_result is not None:
print("not none")
else:
project_team_result = User(login=session['login'], project_session=project_team)
db.session.add(project_team_result)
db.session.commit()

LDAP3 Query will not return login name

The following ldap3 query in py 3.5.3
base_dn = 'OU=Groups,OU=[SOME0U],OU=[ANOTHEROU],DC=BIG,DC=FOOT,DC=COM'
query_filter = '(&(objectCategory=*)(cn=AD_GROUP_NAME))'
attrs=['member']
conn.search(base_dn,query_filter,search_scope=ldap3.SUBTREE, attributes=attrs, size_limit=10)
entry = conn.entries[0]
returns
DN: CN=AD_GROUP_NAME,OU=Groups,OU=SOMEOU,OU=ANOTHEROU,DC=BIG,DC=FOOT,DC=COM - STATUS: Read - READ TIME: 2017-12-19T07:04:45.583177
member: CN=SASQUACH IS REAL,OU=ONEMOREOU,OU=Users,OU=SOMEOU,OU=ANOTHEROU,DC=BIG,DC=FOOT,DC=COM
CN=AD_GROUP_NAME,OU=Groups,OU=SOMEOU,OU=ANOTHEROU,DC=BIG,DC=FOOT,DC=COM
I've tried userPrincipalName, email, givenName,sAMAccountName in the attrs to return, no dice.
I'm needing ONLY the login names to parse into a list (csv) for use elsewhere. Thanks!
IF I UNDERSTAND what you are trying to accomplish.
You need the full dn for the group.
base_dn = 'CN=Users,DC=BIG,DC=FOOT,DC=COM'
query_filter = '(memberOf=CN=YOUR_GROUP_NAME,OU=Groups,OU=[SOME0U],OU=[ANOTHEROU],DC=BIG,DC=FOOT,DC=COM)'
attrs=['sAMAccountName']
results = (base_dn,query_filter,search_scope=ldap3.SUBTREE, attributes=attrs, size_limit=10)
print results

getting an attribute from dictionary object django

I have a dictionary object namely person.json:
def get_web_parent(self, pk, is_ajax=False):
try:
person = models.Person.objects.active.get(pk=pk)
description = person.json
person.json is returning a dictionary like {dict}{'description':'example'}
How do I access description value.
I tried person.json.description but no luck.
you can use person.json.get("description", <<other value if key is missing>>)
assuming you get a Dict object.
If person.json is a JSON string you might need to use
person_dict = json.loads(person.json)
and then access it as
person_dict.get("description", "")

Categories