@@ -35,6 +35,7 @@ def put_resource(id):
35
35
36
36
def update_resource (id , json , db ):
37
37
resource = Resource .query .get (id )
38
+ api_key = g .auth_key .apikey
38
39
39
40
if not resource :
40
41
return redirect ('/404' )
@@ -54,11 +55,12 @@ def get_unique_resource_languages_as_strings():
54
55
55
56
try :
56
57
logger .info (
57
- f"Updating resource. Old data: { json_module .dumps (resource .serialize )} " )
58
+ f"Updating resource. Old data: "
59
+ f"{ json_module .dumps (resource .serialize (api_key ))} " )
58
60
if json .get ('languages' ) is not None :
59
61
old_languages = resource .languages [:]
60
62
resource .languages = langs
61
- index_object ['languages' ] = resource .serialize ['languages' ]
63
+ index_object ['languages' ] = resource .serialize ( api_key ) ['languages' ]
62
64
resource_languages = get_unique_resource_languages_as_strings ()
63
65
for language in old_languages :
64
66
if language .name not in resource_languages :
@@ -99,7 +101,9 @@ def get_unique_resource_languages_as_strings():
99
101
db .session .commit ()
100
102
101
103
return utils .standardize_response (
102
- payload = dict (data = resource .serialize ),
104
+ payload = dict (
105
+ data = resource .serialize (api_key )
106
+ ),
103
107
datatype = "resource"
104
108
)
105
109
@@ -124,19 +128,24 @@ def change_votes(id, vote_direction):
124
128
@latency_summary .time ()
125
129
@failures_counter .count_exceptions ()
126
130
@bp .route ('/resources/<int:id>/click' , methods = ['PUT' ])
131
+ @authenticate (allow_no_auth_key = True )
127
132
def update_resource_click (id ):
128
133
return add_click (id )
129
134
130
135
131
- def update_votes (id , vote_direction ):
136
+ def update_votes (id , vote_direction_attribute ):
132
137
resource = Resource .query .get (id )
133
138
134
139
if not resource :
135
140
return redirect ('/404' )
136
141
137
- initial_count = getattr (resource , vote_direction )
138
- opposite_direction = 'downvotes' if vote_direction == 'upvotes' else 'upvotes'
139
- opposite_count = getattr (resource , opposite_direction )
142
+ initial_count = getattr (resource , vote_direction_attribute )
143
+ vote_direction = vote_direction_attribute [:- 1 ]
144
+
145
+ opposite_direction_attribute = 'downvotes' \
146
+ if vote_direction_attribute == 'upvotes' else 'upvotes'
147
+ opposite_direction = opposite_direction_attribute [:- 1 ]
148
+ opposite_count = getattr (resource , opposite_direction_attribute )
140
149
141
150
api_key = g .auth_key .apikey
142
151
vote_info = VoteInformation .query .get (
@@ -152,25 +161,27 @@ def update_votes(id, vote_direction):
152
161
)
153
162
new_vote_info .voter = voter
154
163
resource .voters .append (new_vote_info )
155
- setattr (resource , vote_direction , initial_count + 1 )
164
+ setattr (resource , vote_direction_attribute , initial_count + 1 )
156
165
else :
157
166
if vote_info .current_direction == vote_direction :
158
- setattr (resource , vote_direction , initial_count - 1 )
159
- setattr (vote_info , 'current_direction' , ' None' )
167
+ setattr (resource , vote_direction_attribute , initial_count - 1 )
168
+ setattr (vote_info , 'current_direction' , None )
160
169
else :
161
- setattr (resource , opposite_direction , opposite_count - 1 ) \
170
+ setattr (resource , opposite_direction_attribute , opposite_count - 1 ) \
162
171
if vote_info .current_direction == opposite_direction else None
163
- setattr (resource , vote_direction , initial_count + 1 )
172
+ setattr (resource , vote_direction_attribute , initial_count + 1 )
164
173
setattr (vote_info , 'current_direction' , vote_direction )
165
174
db .session .commit ()
166
175
167
176
return utils .standardize_response (
168
- payload = dict (data = resource .serialize ),
169
- datatype = "resource" )
177
+ payload = dict (data = resource .serialize (api_key )),
178
+ datatype = "resource"
179
+ )
170
180
171
181
172
182
def add_click (id ):
173
183
resource = Resource .query .get (id )
184
+ api_key = g .auth_key .apikey if g .auth_key else None
174
185
175
186
if not resource :
176
187
return redirect ('/404' )
@@ -180,5 +191,5 @@ def add_click(id):
180
191
db .session .commit ()
181
192
182
193
return utils .standardize_response (
183
- payload = dict (data = resource .serialize ),
194
+ payload = dict (data = resource .serialize ( api_key ) ),
184
195
datatype = "resource" )
0 commit comments