Sharepoint Office365 REST Python Client get users - python

I am trying to integrate SharePoint in one of my Python Scripts and I need to grab the users from a SP list. Until now, I managed to log in, search for a specific list, and enumerate all of its items.
def enum_items(list):
items = list.items # .top(1220)
ctx.load(items)
ctx.execute_query()
for index, item in enumerate(items):
print("{0}: {1}".format(index, item.properties['Email']))
The 'Email' column's value is set to User.email like so:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"txtContent": "[$User.email]"
}
Basically the Email is automatically set when a new user is added in the User column.
Some entries where manually added, and others use the above code. For those that were manually added it prints the email, but for the other, it prints: "None".
If I change the 'Email' to 'User', I get the following error, like it cannot find that column or so:
print("{0}: {1}".format(index, item.properties['User']))
KeyError: 'User'
Is there anyway I can grab the USER object, and then use its properties (which ever those are) to grab some information about it?
Thanks!

Related

Download shareplum List from Sharepoint, to upload It again

I'm sucessfully getting a list from shareplum, and using "UpdateListItems" method to update a value.
sharepoint_site = authenticate(MY_SHAREPOINT_URL, MY_SHAREPOINT_SITE, MY_USERNAME, MY_PASSWORD)
sharepoint_list = get_sp_list(sharepoint_site , MY_SHAREPOINT_LIST)
# "Name" is the column that I am trying to update
data = [{'ID': "13", 'Name': 'Teste Python'}]
sharepoint_list.UpdateListItems(data=data, kind='Update')
time.sleep(3)
# at this point, my sharepoint online list is just the same as It was
I'm using ID as the index of certain row that I want to update (I'm trying to update the 13th element). But once I update the value, nothing happens on online sharepoint. The idea I had is that I would do some integration with other local databases, and use that data to upload certain things to a List (a csv/excel file) that is shared on Sharepoint. Currently my users are manually updating things on that sharepoint online list.
Is there any other command I should use to actually upload my new list to online sharepoint?
The 13th element doesn´t necessarily have ID=13 in SharePoint. If you remove items from a SharePoint list that ID is gone forever. Check if you got the right ID.
Also check that the ínternal name of the column (field) really is "Name". Go to list settings and click on the column and look at the URL and it will look something like this towards the end:
/_layouts/15/FldEdit.aspx?List=%7B681071B5-D6BB-4345-894A-69C2D9E27A3C%7D&Field=Prio
The internal name is what comes after Field= and it can be something else than you see when you view the list.

Tweepy: about accessing the "id" of a user after a Pagination response

I'm really stuck on this one.
I'm using Tweepy to get the IDs of all users that liked a specific tweet. I seem to get a list of "User" structures that contain "id", "name" and "username", but I'm not able to get only the "id".
The code is simple:
client = tweepy.Client(
bearer_token=bearer_token,
consumer_key=api_key, consumer_secret=api_secret,
access_token=user_token, access_token_secret=user_token_secret,
wait_on_rate_limit=True
)
for response in tweepy.Paginator(client.get_liking_users, id=tweetid, max_results=100, limit=10):
for item in response:
print("ITEM:\n", item)
if item is not None:
for user in item:
if user is not None:
print(user)
The print of "item" gets me this (simplified, of course; the number of structures is high, that's why I have to use Paginator):
[<User id=0000001 name=user1 username=UserName1>, <User id=0002 name=user2 username=UserName2>, <User id=000003 name=user3 username=UserName3>]
and the print of "user" just gets me the individual usernames: "UserName1", etc.
But no way to get user.id, user.User.id, nor anything similar. And I'm frustrated, because the information is right there, just I can't access it easily.
Thank you!
Tweepy documentation provides an example of something very similar to what you want to do: https://docs.tweepy.org/en/stable/examples.html -> API v2 -> Get Tweet’s Liking Users
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get Tweet's Liking Users
# This endpoint/method allows you to get information about a Tweet’s liking
# users
tweet_id = 1460323737035677698
# By default, only the ID, name, and username fields of each user will be
# returned
# Additional fields can be retrieved using the user_fields parameter
response = client.get_liking_users(tweet_id, user_fields=["profile_image_url"])
for user in response.data:
print(user.username, user.profile_image_url)
This example prints the user's username and profile image URL, but note the comment says the id is also returned, so something like user.id should work. Otherwise, you can also add id to user_fields to make sure it's returned, although that shouldn't be necessary.
Unfortunately, I am not able to test it myself because I don't have a Twitter developer account with the required elevated access.
Edit: I got access to an API account with elevated access and I was able to test your code, see the update below
Iterating paginated results
The reason why you need a double for loop to iterate the paginated results and it eventually crashes after showing some results with an error saying you are trying to access a non-existent id attribute on an str object is because you are not iterating the Paginator results correctly.
For the sake of simplicity, I'm going to label your three nested for loops:
loop 0: for response in tweepy.Paginator(...
loop 1: for item in response
loop 2: for user in item
Paginator returns a Response object with all the results in the data attribute. The object has other attributes like meta, count, etc.
When you do loop 1, you are iterating all these data, count, etc., attributes of Response.
If the attribute you are iterating happens to be the data attribute, it will start loop 2 and it will iterate the results getting the output you expect.
But loop 1 will also iterate other Reponse items outside of the data attribute.
Let's see, for example, what happens when loop 1 enters the meta attribute.
meta is a dictionary that looks like this:
meta={'result_count': 80, 'next_token': '676f9b7bumw8i3jbm4nnifamw2ejjaktp8kjym6akdak9'}
When you enter loop 2 with the meta attribute, it will start iterating the keys (not the values, because that's how dicts work in Python) so the value of user in loop 2 will be either result_count or next_token. And it's then when you are getting your error saying you are trying to access id on a str.
What you should be doing is iterating the response.data in loop 1 instead and that will also allow removing the need of a second loop:
for response in tweepy.Paginator(client.get_liking_users, id=tweetid, max_results=100, limit=10):
for user in response.data:
print(user.id)
Edit: grammar and style

Odoo API write method

I've read other topics and also the full documentation.
I'm trying to write to a record.
API returns "True", but there is no effect to the field.
write_to_db = models.execute_kw(db, uid, password,
'mail.mass_mailing.contact', 'write', [ [the_contact[0]["id"]], { 'tag_ids': the_contact[0]["tag_ids"]}])
the_contact[0]["id"] is the id of the contact whose tags I want to change (add a new tag).
the_contact[0]["tag_ids"] contains [1,2] ==> the new list of tags
I'm aware that for one2many and many2many fields I need special syntax:
https://www.odoo.com/documentation/10.0/reference/orm.html
From (0, _, values) ... to (6, _, ids)
But some of those syntax don't even has a place for new values!!! Just options 0 and 1... for the others one have just place to id??? But id is already in the command...
Tried many combinations but none worked.
(Bottom line: I'm a beginner in both Odoo and Python)
Got the answer.
The problem was not the syntax.
"tag_ids" in fact are related to res.partner, not to mail.mass_mailing.tag

python database table linking

I'm new to Python and I'm trying to make a simple bulletin board system app using web2py. I am trying to add a post into a certain board and I linked the post and board by including the following field in my post table: Field('board_id', db.board). When I try to create a post inside a particular board it gives me an error: "OperationalError: no such column: board.id". My code for create_posts:
def add_post():
board = db.board(request.args(0))
form = SQLFORM(db.post)
db.pst.board_id.default = db.board.id
if form.process().accepted:
session.flash = T('The data was inserted')
redirect(URL('default', 'index'))
return dict(form=form, board=board)
When I try to do {{=board}} on the page that shows the posts in a certain board, I get Row {'name': 'hi', 'id': 1L, 'pst': Set (pst.board_id = 1), 'description': 'hi'} so I know it's there in the database. But when I do the same thing for the "add post" form page, it says "board: None". I'm extremely confused, please point me in the right direction!
There appear to be several problems with your function. First, you are assigning the default value of the board_id field to be a Field object (i.e., db.board.id) rather than an actual id value (e.g., board.id). Second, any default values should be assigned before creating the SQLFORM.
Finally, you pass db.post to SQLFORM, but in the next line, the post table appears to be called db.pst -- presumably these are not two separate tables and one is just a typo.
Regarding the issue of {{=board}} displaying None, that indicates that board = db.board(request.args(0)) is not retrieving a record, which would be due to request.args(0) itself being None or being a value that does not match any record id in db.board. You should check how you are generating the links that lead to add_post and confirm that there is a valid db.board id in the first URL arg. In any case, it might be a good idea to detect when there is no valid board record and either redirect or display an error message.
So, your function should look something like this:
def add_post():
board = db.board(request.args(0)) or redirect(URL('default', 'index'))
db.pst.board_id.default = board.id
form = SQLFORM(db.pst)
if form.process(next=URL('default', 'index'),
message_onsuccess=T('The data was inserted'))
return dict(form=form, board=board)
Note, if your are confident that links to add_post will include valid board IDs, then you can eliminate the first line altogether, as there is no reason to retrieve a record based on its ID if the only field you need from it is the ID (which you already have). Instead, the second line could be:
db.pst.board_id.default = request.args(0) or redirect(URL('default', 'index'))

Django insert into list if doesn't contain it

I want to insert into the notifications list, one notification of each type. and I have this:
Result from the initial query (I guess is a list with queryset), named notifications:
[<Notification: Should be first>, <Notification: New link>]
And the restriction that I have is:
for(note in notification):
if len(note.region.all()) == 0 and (note.notificationType.priority not in notifications):
notifications.append(note)
My question is, how do I get inside the notifications list to get the attribute notificationType.priority to see is that number isn't inside notifications list.
If I get your question, you can try this :
notificationsPiorities = set([note.notificationType.priority for note in notifications ])
for(note in notification):
if len(note.region.all()) == 0 and (note.notificationType.priority not in notificationsPiorities ):
notifications.append(note)
notificationsPiorities.add(note.notificationType.priority)
Also, you may need to rename your variables. I can't tell for sure what are notification and notifications. One is the list you will display, and one is the list you retrieve with your query ?

Categories

Resources