Skip to content
This repository was archived by the owner on Feb 27, 2021. It is now read-only.

Commit 0deb692

Browse files
committed
button to claim/unclaim paper for user profile
1 parent 643a192 commit 0deb692

File tree

8 files changed

+213
-5
lines changed

8 files changed

+213
-5
lines changed

papers/ajax.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,44 @@ def harvestingStatus(request, pk):
126126
return resp
127127

128128

129+
@user_passes_test(is_authenticated)
130+
@json_view
131+
@require_POST
132+
def claimPaper(request):
133+
"""claim paper pk for current user"""
134+
success = False
135+
try:
136+
paper = Paper.objects.get(pk=int(request.POST["pk"]))
137+
except (KeyError, ValueError, Paper.DoesNotExist):
138+
return {'success': success, 'message': 'Invalid paper id'}, 404
139+
try:
140+
# returns true or false depending on whether something was actually
141+
# changed
142+
paper.claim_for(request.user)
143+
except ValueError:
144+
# paper cannot be claimed
145+
return {'success': success,
146+
'message': 'Paper cannot be claimed by user'}, 403
147+
success = True
148+
return {'success': success}
149+
150+
151+
@user_passes_test(is_authenticated)
152+
@json_view
153+
@require_POST
154+
def unclaimPaper(request):
155+
"""unclaim paper pk for current user"""
156+
success = False
157+
try:
158+
paper = Paper.objects.get(pk=int(request.POST["pk"]))
159+
except (KeyError, ValueError, Paper.DoesNotExist):
160+
return {'success': success, 'message': 'Invalid paper id'}, 404
161+
# returns true or false depending on whether something was actually changed
162+
paper.unclaim_for(request.user)
163+
success = True
164+
return {'success': success}
165+
166+
129167
@user_passes_test(is_authenticated)
130168
@json_view
131169
def waitForConsolidatedField(request):
@@ -227,4 +265,8 @@ def get_queryset(self):
227265
name='ajax-setResearcherDepartment'),
228266
url(r'^institutions.geojson', InstitutionsMapView.as_view(),
229267
name='ajax-institutions-geojson'),
268+
url(r'^claim-paper$',
269+
claimPaper, name='ajax-claimPaper'),
270+
url(r'^unclaim-paper$',
271+
unclaimPaper, name='ajax-unclaimPaper'),
230272
]

papers/baremodels.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,10 @@ def serialize(self):
804804
'full': self.full,
805805
}
806806

807+
@property
808+
def pair(self):
809+
return (self.first, self.last)
810+
807811
@classmethod
808812
def deserialize(cls, rep):
809813
"""

papers/models.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,14 +911,90 @@ def can_be_asked_for_upload(self):
911911
@cached_property
912912
def owners(self):
913913
"""
914-
Returns the list of users that own this papers (listed as authors and identified as such).
914+
Returns the list of users that own this paper (listed as authors and identified as such).
915915
"""
916916
users = []
917917
for a in self.authors:
918918
if a.researcher and a.researcher.user:
919919
users.append(a.researcher.user)
920920
return users
921921

922+
def can_be_claimed_by(self, user):
923+
"""
924+
Is it possible for user to claim this paper?
925+
"""
926+
for author in self.authors:
927+
first_name = None
928+
last_name = None
929+
try:
930+
first_name = user.first_name
931+
last_name = user.last_name
932+
except AttributeError:
933+
# anonymous user
934+
return False
935+
if not match_names(author.name.pair,
936+
(first_name, last_name)):
937+
continue
938+
if author.orcid:
939+
continue
940+
if author.researcher_id:
941+
continue
942+
return True
943+
return False
944+
945+
def unclaim_for(self, user):
946+
"""
947+
Remove all associations between this paper and user
948+
"""
949+
try:
950+
user_researcher = Researcher.objects.get(user=user)
951+
except Researcher.DoesNotExist:
952+
user_researcher = None
953+
user_orcid = None
954+
if user_researcher:
955+
user_orcid = user_researcher.orcid
956+
for idx, author in enumerate(self.authors):
957+
if (author.orcid == user_orcid or author.researcher_id ==
958+
user_researcher.id):
959+
# remove association between this author and user
960+
self.authors_list[idx]['orcid'] = None
961+
self.authors_list[idx]['researcher_id'] = None
962+
self.save()
963+
self.update_index()
964+
return True
965+
# nothing was done, paper cannot be unclaimed
966+
raise False
967+
968+
def claim_for(self, user):
969+
"""
970+
Associate this paper to the requested user
971+
"""
972+
if user in self.owners:
973+
# user already owns the paper
974+
return False
975+
try:
976+
user_researcher = Researcher.objects.get(user=user)
977+
except Researcher.DoesNotExist:
978+
user_researcher = None
979+
user_orcid = None
980+
if user_researcher:
981+
user_orcid = user_researcher.orcid
982+
for idx, author in enumerate(self.authors):
983+
if not match_names(author.name.pair,
984+
(user.first_name, user.last_name)):
985+
continue
986+
if author.orcid:
987+
continue
988+
if author.researcher_id:
989+
continue
990+
self.authors_list[idx]['orcid'] = user_orcid
991+
self.authors_list[idx]['researcher_id'] = user_researcher.id
992+
self.save()
993+
self.update_index()
994+
return True
995+
# paper cannot be claimed by user
996+
raise ValueError
997+
922998
def is_owned_by(self, user, flexible=False):
923999
"""
9241000
Is this user one of the owners of that paper?

papers/templates/papers/paperList.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{% endif %}
1616
<ul class="withoutTriangles">
1717
{% endifchanged %}
18-
<li class="pubItem">
18+
<li class="pubItem" id="paper-{{ paper.id }}">
1919
{% if paper.pk %}
2020
{% include "papers/publiListItem.html" with paper=paper with_buttons=request.user.is_authenticated %}
2121
</li>

papers/templates/papers/publiListItem.html

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{% load author %}
22
{% load i18n %}
33
{% load cache %}
4+
{% load canclaim %}
45
{% load domain %}
56
{% get_current_language as LANGUAGE_CODE %}
6-
{% cache 60000 publiListItem paper.pk LANGUAGE_CODE researcher_id %}
77
<div class="pubLogo logoHelpPopover" data-content="{{ paper.status_helptext }}" rel="popover"
88
data-placement="bottom">
99
<img src="/static/img/logos/{{ paper.combined_status }}.png" width="52" height="70" />
1010
</div>
1111

1212
<div class="publicationText">
13+
{% cache 60000 publiListItem paper.pk LANGUAGE_CODE researcher_id %}
1314
<p class="paperAuthors">
1415
{% include "papers/authorList.html" with author_list=paper.displayed_authors %}
1516
{% if paper.has_many_authors %}
@@ -29,6 +30,7 @@
2930
</p>
3031
{% endif %}
3132
{% endwith %}
33+
{% endcache %}
3234

3335
<p class="paperDownload">
3436
{% if paper.pdf_url %}
@@ -37,6 +39,18 @@
3739
{% else %}
3840
<a href="{% url 'upload_paper' paper.pk %}" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-open"></span> {% trans "Upload" %}</a>
3941
{% endif %}
42+
{% if request.user in paper.owners %}
43+
<a class="btn btn-default btn-xs unclaim-button"
44+
data-pk="{{ paper.id }}" data-action="unclaim">
45+
{% trans "Exclude from my profile" %}
46+
</a>
47+
{% else %}
48+
{% canclaim request.user paper as can_claim %}
49+
{% if can_claim %}
50+
<a class="btn btn-default btn-xs claim-button"
51+
data-pk="{{ paper.id }}" data-action="claim">
52+
{% trans "Include in my profile" %}</a>
53+
{% endif %}
54+
{% endif %}
4055
</p>
4156
</div>
42-
{% endcache %}

papers/templates/papers/search.html

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,63 @@
4444
isSuperUser: {{ request.user.is_superuser|yesno:"true,false,false" }},
4545
initialMessages: {{ messages | safe }}
4646
}
47-
init_paper_module(config)
47+
init_paper_module(config);
48+
function claim_unclaim (evt) {
49+
var obj = $(evt.target);
50+
var pk = obj.attr("data-pk");
51+
var action = obj.attr("data-action");
52+
if (action != "claim" && action != "unclaim") {
53+
// action currently ongoing
54+
return;
55+
}
56+
var my_url = "";
57+
if (action == "claim") {
58+
obj.text("{% trans "Claiming..." %}");
59+
my_url = "{% url "ajax-claimPaper" %}";
60+
} else {
61+
obj.text("{% trans "Unclaiming..." %}");
62+
my_url = "{% url "ajax-unclaimPaper" %}";
63+
}
64+
obj.toggleClass("disabled");
65+
$.ajax({
66+
method: "post",
67+
url: my_url,
68+
data: {"pk": pk,
69+
"csrfmiddlewaretoken": '{{csrf_token}}'},
70+
cache: false,
71+
success: function (data) {
72+
if (action == "claim") {
73+
obj.text("{% trans "Exclude from my profile" %}");
74+
obj.attr("data-action", "unclaim");
75+
} else {
76+
obj.text("{% trans "Include in my profile" %}");
77+
obj.attr("data-action", "claim");
78+
{% if user_researcher == researcher %}
79+
// user_researcher is only defined on profile views
80+
// in which case it is equal to the researcher of the currently
81+
// logged in user; if it is the profile currently being viewed,
82+
// we must remove the paper entirely
83+
console.log("#paper-" + pk);
84+
$("#paper-" + pk).fadeOut(300, function() { $(this).remove(); });
85+
{% endif %}
86+
}
87+
obj.toggleClass("claim-button");
88+
obj.toggleClass("unclaim-button");
89+
obj.toggleClass("disabled");
90+
},
91+
fail: function(data) {
92+
if (action == "claim") {
93+
obj.text("{% trans "Claiming failed!" %}");
94+
} else {
95+
obj.text("{% trans "Unclaiming failed!" %}");
96+
}
97+
obj.toggleClass("disabled");
98+
}
99+
});
100+
}
101+
102+
$(".unclaim-button").click(claim_unclaim);
103+
$(".claim-button").click(claim_unclaim);
48104
});
49105
{% endblock %}
50106

papers/templatetags/canclaim.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- encoding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django import template
5+
6+
register = template.Library()
7+
8+
9+
@register.simple_tag(name="canclaim")
10+
def canclaim(user, paper):
11+
return paper.can_be_claimed_by(user)

papers/views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ def get(self, request, *args, **kwargs):
229229
def get_context_data(self, **kwargs):
230230
context = super(ResearcherView, self).get_context_data(**kwargs)
231231
researcher = self.researcher
232+
# researcher corresponding to the currently logged in user
233+
try:
234+
context['user_researcher'] = Researcher.objects.get(user=self.request.user)
235+
except Researcher.DoesNotExist:
236+
pass # no logged in user
232237
context['researcher'] = researcher
233238
context['researcher_id'] = researcher.id
234239
context['search_description'] += _(' authored by ')+unicode(researcher)

0 commit comments

Comments
 (0)