PyMongo - "language override unsupported: C++" when creating index - python

I have the collection posts that contains posts that look something like this
{
"_id": "5ae37fd270f3e72399988198",
"moderator": {
"flagged": false,
"reviewed": true,
"pending": false,
"time": "2018-04-27 20:34:38.099000",
"account": "samhamou"
},
"author": "cryptohazard",
"permlink": "security-enhancements-for-steem-messenger",
"title": "Security enhancements for Steem Messenger",
"repository": {
"owner": {
"login": "kingswisdom"
},
"fork": false,
"html_url": "https:\/\/github.com\/kingswisdom\/SteemMessenger",
"full_name": "kingswisdom\/SteemMessenger",
"name": "SteemMessenger",
"id": 127418766
}
}
I am trying to create an index on the collection in one of my Python files with the following code
posts = DB.posts
posts.drop_indexes()
posts.create_index([
("author", "text"),
("moderator.account", "text"),
("repository.full_name", "text")
])
but this is giving me the following error:
pymongo.errors.OperationFailure: language override unsupported: C++
How can I prevent this from happening? I can create the indexes using code found in the answers of this question:
> db.posts.createIndex({
"moderator.account": "text",
author: "text",
"repository.full_name": "text"
}, {
"language_override": "en"
});
However I want to be able to do it from within my Python script instead of having to do it from the MongoDB shell. I tried finding a way to add the "language_override": "en" option from within my Python script, but this doesn't seem possible when checking the documentation for create_index.

Related

Build a JSON object using for loop with python

I am new to python and trying to write a simple script that pushes notification to my slack channel about open tickets in my queue.
I am trying to submit the below JSON object to a slack webhook but not sure how to achieve the end result that looks like below.
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "There are *2* open tickets :in the queue\n *Ticket Details *"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*12345* - High CPU usage on prod app1 server"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*54321* - DB parameter modification - ProdDB03"
}
},
{
"type": "divider"
}
]
}
I am doing an API call to an URL that gives me the open ticket details in a JSON format. I am able to parse through it and store the Count (number of open tickets), Ticket ID and the Ticket Description in respective variables.
I would like to create a block for each ticket as below.
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*54321* - DB parameter modification - ProdDB03"
}
}
Just ideas on how to achieve this without actual code would be helpful too.
Thanks

pymsteams. I need to mention a person in message sent by pymsteams. How to do it?

I have installed and connected pymsteams to a channel and able to send messages. I read documentation but didn't find anything related to how to send message with mentioning a person as #me -> mentioning a person in a teams.
that's a beginning of my code:
import pymsteams;
myMessage = pymsteams.connectocard("webhookurl")
.... // here is a logic
myMessage.send()
I believe you can use an AC card. Here is a detailed explanation where I learned it.
There is an issue thread on github requesting exactly this kind of functionality
https://github.com/rveachkc/pymsteams/issues/11
and there is a comment that presents a temporary solution, which is to manually modify the payload attribute of the connectorcard object. This worked for me.
teams_message = pymsteams.connectorcard(webhook)
teams_message.payload = {
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"text": "Hello <at>Leonardo</at>"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0",
"msteams": {
"entities": [
{
"type": "mention",
"text": "<at>Leonardo</at>",
"mentioned": {
"id": "leonardo.benitez#contorso.com",
"name": "Leonardo Benitez"
}
}
]
}
}
}]
}
teams_message.send()
The code snippet, thanks to the author for providing it.

Location widget in messenger platform displays a search bar in mobile version but not in Desktop Version

By using bot functionality provided by facebook messenger platform,
I want users to be able to provide location by using search.
It's working as expected in mobile app of the messenger as it's showing search option. But in the desktop version of the messenger, search option is not showing in location widget.
I wanted to ask that is it expected behavior or I'm missing out something.
Also in browser console, it's showing some error:
ErrorUtils caught an error:
"navigator.permissions.query(...).then(...).done is not a function".
Subsequent errors won't be logged;
see https://fburl.com/debugjs.ja # qtaMy7UNoCv.js:47.
Here's what I've tried so far:
def send_location_with_quick_reply(self, recipient_id, message, quick_replies):
url = self.get_message_url()
payload = {
"recipient":{
"id":recipient_id
},
"message":{
"text": message,
"quick_replies":[{
"content_type": "location"
}]
}
}
# _do_post will hit the send_message API of `Messenger`.
return self._do_post(url, payload)
And here's the response I'm getting after the user chooses the location:
{
"object": "page",
"entry": [{
"id": "128922990987384",
"time": 1505890084176,
"messaging": [{
"sender": {
"id": "1456347437763847"
},
"recipient": {
"id": "128922990987384"
},
"timestamp": 1505890084065,
"message": {
"mid": "mid.$cAAAvskrTvY9kz1Bs4Vengsjpb9L_",
"seq": 2366,
"attachments": [{
"title": "User's Location",
"url": "https:\\/\\/l.facebook.com\\/l.php?u=https\\u00253A\\u00252F\\u00252Fwww.bing.com\\u00252Fmaps\\u00252Fdefault.aspx\\u00253Fv\\u00253D2\\u002526pc\\u00253DFACEBK\\u002526mid\\u00253D8100\\u002526where1\\u00253D12.9703749\\u0025252C\\u00252B77.6361206\\u002526FORM\\u00253DFBKPL1\\u002526mkt\\u00253Den-US&h=ATNsjbke0tPFGIFpCq4MA5l1W6wmiwp0cTfUZNXSSbMDHxygEM4GrVlZmtaebsN4elliFhMSJNmIFwQgn-p_fxnF2hW0VdKTj2z_0hsWnH4dlLZGdQ&s=1&enc=AZN9DwrutbtXSfRAdxQf4bzFSMSO3zujAb0LBOgUt9mz16ZnDn7CSZDBLmnISjfAMbLG6b6H6hn9a3KCb6wOo7dn",
"type": "location",
"payload": {
"coordinates": {
"lat": 12.9703749,
"long": 77.6361206
}
}
}]
}
}]
}]
}
I am using python and drf to integrate with messenger platform.
Yes, this is expected behavior currently.

Ember serializer customization for alternate url

I am a newbie to ember. So here is what i faced. I got backend RESTAPI written in python/Django. It provides following json response on /api/works/
[
{
"id": 17,
"title": "about us",
"description": "some project",
"owner": "admin"
},
{
"id": 19,
"title": "test1 profit project",
"description": "dev null",
"owner": "test1"
}
]
also on detail view E.g:/api/works/17/:
{
"id": 17,
"title": "about us",
"description": "some project",
"owner": "admin"
}
there is also /api/works/17/tasks/ for listing work tasks
[
{
"id": 2,
"title": "secondWorkTask",
"description": "task2 description",
"due_date": "2016-09-26",
"assign": 1,
"project": "about us"
},
{
"id": 3,
"title": "some task name",
"description": "some task description",
"due_date": "2016-08-27",
"assign": 2,
"project": "about us"
}
]
on the front-end side i am using ember-cli version2.7.0 + ember-django-adapter. I can get /api/works without problem.
serializer on ember to get project:
export default DRFSerializer.extend({
normalizeFindAllResponse(store, primaryModelClass, payload, id, requestType) {
payload.data = payload;
return this._super(...arguments);
}
});
What i want to achieve is on the ember side when work detail url on the ember side(emberexample-app.com/app/17/) load, it must show all tasks. Currently i can get work detail by this url /api/works/17/ with above serializer. But how can i get tasks? Please help me to find a way to solve this.
The serializer are used to customize the loading and saving (or serialization and deserialization) of data.
To customize the URLs you must use an Adapter,(e.g. RESTAdapter is my most used adapter).
That will work in the case you want to create (urlForCreateRecord) or update (urlForUpdateRecord) tasks but it may not directly work if you just want to convert a work.get('tasks') following a belongsTo relationship to a GET http://endpoint/work/<work_id>/tasks, at least in my case it didn't "just work"ed.
A solution I found that works very well is adding a property links as a plain object to the parent objects that contains links to the different child models you want to retrieve as properties.
Something like this:
/* app/adapters/work.js */
import DS from 'ember';
export default DS.RESTSerializer({
normalize (model, hash, prop) {
hash.links = hash.links || {};
hash.links.tasks = this.buildURL(model, hash.id) + '/tasks';
return this._super(model, hash, prop);
}
});
In this example, when Ember try to get a tasks property from a work model, it will make a GET to the URL that work.links.tasks contains.

JSON Type Validation - Guidance

I have a stream of events coming in as JSON. The schema for the JSON is well defined, but the source producing them doesn't always behave when it comes to types.
Example Schema:
{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"properties":{
"FirstName": {
"type":"string",
"id": "http://jsonschema.net/FirstName",
"required":false
},
"MiddleName": {
"type":"string",
"id": "http://jsonschema.net/MiddleName",
"required":false
},
"LastName": {
"type":"string",
"id": "http://jsonschema.net/LastName",
"required":false
},
"Age": {
"type":"number",
"id": "http://jsonschema.net/Age",
"required":false
}
}
In some cases the Age shows up as a "-" character, meaning it was left blank when the record was created. Obviously this isn't a number, thus my problem.
I'm not using any formal JSON validation library, but I was considering looping through each element of the event and handling any needed type conversation. In the example above, I would just make age 0.
Is there a way to validate each element and then apply some type of conversation function is it fails validation?
I ended up using Schematics with custom Types to do this. Works perfectly.

Categories

Resources