View difference between Paste ID: Faugbjms and CHw3njyZ
SHOW: | | - or go back to the newest paste.
1-
function submit_like(e, post_pk) {
1+
from pymongo import MongoClient
2-
    let like_button = document.querySelector('#like_button');
2+
from bson import ObjectId
3-
    let button_value;
3+
4
client = MongoClient("localhost", 27017)
5-
    if(like_button.className.includes("creative")){
5+
db = client["MovieLibrary"]
6-
        like_button.className = 'like_button';
6+
collection = db["movies"]
7-
        button_value = 0;
7+
8-
    }else{
8+
# CRUD
9-
        like_button.className = 'like_button creative'
9+
10-
        button_value = 1;
10+
# create data
11-
    }
11+
# collection.insert_one(
12
#     {
13-
    let like_text_element = document.querySelector('#like_button_text');
13+
#         "title":"Terminator 2",
14-
    let current_likes = parseInt(like_text_element.textContent,10);
14+
#         "language": "eng"
15-
    console.log(like_text_element, current_likes)
15+
#      }
16-
    if(button_value === 1){
16+
# )
17-
        current_likes += 1
17+
#
18-
    }else{
18+
# collection.insert_one(
19-
        current_likes -= 1
19+
#     {
20-
    }
20+
#         "title":"Amelie",
21
#         "language": "fr"
22-
    like_text_element.textContent = current_likes + ' Likes'
22+
#      }
23
# )
24-
    // submit with AJAX
24+
#
25-
    $.ajax({
25+
# collection.insert_one(
26-
        url:"/ajax/like_submit/",
26+
#     {
27-
        data:{
27+
#         "title":"Arrival",
28-
            "value": button_value,
28+
#         "language": "eng"
29-
            "post_pk": post_pk,
29+
#      }
30-
        },
30+
# )
31-
        dataType: 'json',
31+
32
# find data all
33-
        success: function (data) {}
33+
# print([i for i in collection.find()])
34
35-
    });
35+
# find by name
36
# print(collection.find_one({ "title": "Aliens"}))
37-
}
37+
38
# find by id
39
# print(collection.find_one({ "_id": ObjectId('5dbee0fcbdb9649e9fc5b115')}))
40
41
# find by language
42
# print([i for i in collection.find({"language":"eng"})])
43
44
# edit data
45
# collection.update_one({"_id":ObjectId('5dbee0fcbdb9649e9fc5b115')}, {"$set":{"language":"eng"}})
46
47
# delete
48
collection.delete_one({"_id":ObjectId('5dbee0fcbdb9649e9fc5b115')})