diff --git a/community/community/doctype/community_project/community_project.json b/community/community/doctype/community_project/community_project.json index 6e0b4b33..c31019b3 100644 --- a/community/community/doctype/community_project/community_project.json +++ b/community/community/doctype/community_project/community_project.json @@ -67,11 +67,13 @@ { "fieldname": "likes", "fieldtype": "Int", - "label": "Likes" + "label": "Likes", + "read_only": 1 }, { "fieldname": "project_search", "fieldtype": "Small Text", + "hidden": 1, "label": "Project Search" }, { @@ -83,7 +85,7 @@ ], "index_web_pages_for_search": 1, "links": [], - "modified": "2021-02-17 12:41:32.562346", + "modified": "2021-02-23 11:21:47.948253", "modified_by": "Administrator", "module": "Community", "name": "Community Project", diff --git a/community/www/hackathons/project.js b/community/www/hackathons/project.js index 5201974c..0d6dd8bd 100644 --- a/community/www/hackathons/project.js +++ b/community/www/hackathons/project.js @@ -12,11 +12,13 @@ var set_likes = function(liked, likes) { }; // set initial -//set_likes(liked, likes); +/* frappe.call('community.www.hackathons.project.like', {project: frappe.form_dict.project}, (data) => { + set_likes(data.message.action =="Liked", data.message.likes) +}) */ // like - unlike $('.btn-like').on('click', (e) => { - frappe.call('like', {project: $(e.target).attr("data-project")}, (data) => { + frappe.call('community.www.hackathons.project.like', {project: $(e.target).attr("data-project")}, (data) => { set_likes(data.message.action =="Liked", data.message.likes); }); }); diff --git a/community/www/hackathons/project.py b/community/www/hackathons/project.py index 90fc83cf..8b6812f6 100644 --- a/community/www/hackathons/project.py +++ b/community/www/hackathons/project.py @@ -65,4 +65,28 @@ def join_request(id, action): frappe.throw('A project cannot have more than 4 members') frappe.db.set_value('Community Project Member', id, 'status', 'Accepted') else: - frappe.db.set_value('Community Project Member', id, 'status', 'Rejected') \ No newline at end of file + frappe.db.set_value('Community Project Member', id, 'status', 'Rejected') + +def has_already_liked(project): + try: + likes = frappe.get_doc("Community Project Like", {"project": project, "owner": frappe.session.user}) + return likes + except frappe.DoesNotExistError: + return None + +@frappe.whitelist() +def like(project): + liked_project = has_already_liked(project) + if liked_project: + action= "Unliked" + liked_project.delete() + else: + action= "Liked" + frappe.get_doc({"doctype": "Community Project Like","project": project}).save() + + likes = frappe.db.get_all("Community Project Like", {"project": project}) + frappe.db.set_value("Community Project", project, "likes", len(likes)) + return { + "action": action, + "likes": len(likes) + }