Skip to content

Commit

Permalink
Merge pull request #19 from gabrielchl/publishing-edits
Browse files Browse the repository at this point in the history
Publishing edits
  • Loading branch information
gabrielchl committed Aug 17, 2020
2 parents 649334c + e8e2b70 commit f4741f9
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 28 deletions.
4 changes: 2 additions & 2 deletions mdvt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

@babel.localeselector
def get_locale():
locale = db_get_user_setting(session['user_id'], 'locale')
locale = db_get_user_setting(session.get('user_id'), 'locale')
if locale:
return locale
else:
Expand All @@ -32,7 +32,7 @@ def get_locale():

@babel.timezoneselector
def get_timezone():
timezone = db_get_user_setting(session['user_id'], 'timezone')
timezone = db_get_user_setting(session.get('user_id'), 'timezone')
if timezone:
return timezone
else:
Expand Down
94 changes: 77 additions & 17 deletions mdvt/contribute/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

from mdvt.contribute.util import (get_contrib_count, get_questions,
get_test_contrib_count,
get_test_contrib_score, get_test_questions)
get_test_contrib_score, get_test_questions,
make_edit_call)
from mdvt import db
from mdvt.database.models import (Contribution, Question,
TestContribution, TestQuestion, User)
from mdvt.database.util import db_get_user_setting, db_set_or_update_user_setting
from mdvt.main.util import is_logged_in

from datetime import datetime
import json

contribute_bp = Blueprint('contribute', __name__)

Expand Down Expand Up @@ -67,21 +69,36 @@ def api_get_question_text():
'P180': gettext('Is [DEPICT] in the above [MEDIA]?'),
'rank': gettext('Is [DEPICT] porminent in the above [MEDIA]?'),
'P2677': gettext('Is [DEPICT] in the frame in the above [MEDIA]?'),
'P1354': gettext('Is [DEPICT] in the above [MEDIA] shown with [QUALIFIER] (on it)?'),
'P462': gettext('Is [DEPICT] in the above [MEDIA] have the color [QUALIFIER]?'),
'P518': gettext('Is [DEPICT] at the [QUALIFIER] part of the above [MEDIA]?'),
'P1114': gettext('Are there [QUALIFIER] [DEPICT](s) in the above [MEDIA]?'),
'P4878': gettext('Does the [DEPICT] in the above [MEDIA] symbolize [QUALIFIER]?'),
'P3828': gettext('Is [DEPICT] in the above [MEDIA] wearing (a) [QUALIFIER]?'),
'P710': gettext('Is [QUALIFIER] a participant in [DEPICT] in the above [MEDIA]?'),
'P1419': gettext('Is the [DEPICT] in the above [MEDIA] in [QUALIFIER] shape?'),
'P6022': gettext('Is [DEPICT] in the above [MEDIA] having the expression, gesture or body pose [QUALIFIER]?'),
'P186': gettext('Is [QUALIFIER] used in the [DEPICT] in the above [MEDIA]?'),
'P1884': gettext('Does [DEPICT] in the above [MEDIA] have [QUALIFIER]?'),
'P1552': gettext('Is [QUALIFIER] a quality of [DEPICT] in the above [MEDIA]?'),
'P1545': gettext('Does the [DEPICT] in the above [MEDIA] have the series ordinal [QUALIFIER]?'),
'P7380': gettext('Is the [DEPICT] in the above [MEDIA] identified by [QUALIFIER]?'),
'P149': gettext('Is the [DEPICT] in the above [MEDIA] of [QUALIFIER](style)?')
'P1354': gettext('Is [DEPICT] in the above [MEDIA] shown with '
'[QUALIFIER] (on it)?'),
'P462': gettext('Is [DEPICT] in the above [MEDIA] have the color '
'[QUALIFIER]?'),
'P518': gettext('Is [DEPICT] at the [QUALIFIER] part of the above '
'[MEDIA]?'),
'P1114': gettext('Are there [QUALIFIER] [DEPICT](s) in the above '
'[MEDIA]?'),
'P4878': gettext('Does the [DEPICT] in the above [MEDIA] symbolize '
'[QUALIFIER]?'),
'P3828': gettext('Is [DEPICT] in the above [MEDIA] wearing (a) '
'[QUALIFIER]?'),
'P710': gettext('Is [QUALIFIER] a participant in [DEPICT] in the '
'above [MEDIA]?'),
'P1419': gettext('Is the [DEPICT] in the above [MEDIA] in [QUALIFIER] '
'shape?'),
'P6022': gettext('Is [DEPICT] in the above [MEDIA] having the '
'expression, gesture or body pose [QUALIFIER]?'),
'P186': gettext('Is [QUALIFIER] used in the [DEPICT] in the above '
'[MEDIA]?'),
'P1884': gettext('Does [DEPICT] in the above [MEDIA] have '
'[QUALIFIER]?'),
'P1552': gettext('Is [QUALIFIER] a quality of [DEPICT] in the above '
'[MEDIA]?'),
'P1545': gettext('Does the [DEPICT] in the above [MEDIA] have the '
'series ordinal [QUALIFIER]?'),
'P7380': gettext('Is the [DEPICT] in the above [MEDIA] identified by '
'[QUALIFIER]?'),
'P149': gettext('Is the [DEPICT] in the above [MEDIA] of [QUALIFIER] '
'(style)?')
}

return jsonify({
Expand Down Expand Up @@ -177,7 +194,7 @@ def api_contribute():
}
}), 401

if ('csrf' not in session
if ('csrf' not in session or not session['csrf']
or request.get_json()['csrf'] != session['csrf'][1]):
return jsonify({
'status': 'fail',
Expand Down Expand Up @@ -222,6 +239,49 @@ def api_contribute():
}
})
else:
question = (Question.query
.filter(Question.id == contrib_request['question_id'])
.first())
if question.type == 'rank':
current_rank = question.qualifier_value
if ((current_rank == 'normal'
and contrib_request['status'] == 'true')
or (current_rank == 'preferred'
and contrib_request['status'] == 'false')):

if contrib_request['status'] == 'true':
correct_rank = 'preferred'
else:
correct_rank = 'normal'
params = {
'action': 'wbsetclaim',
'format': 'json',
'claim': json.dumps({
"mainsnak": {
"snaktype": "value",
"property": "P180",
"datavalue": {
"value": {
"id": question.depict_value
},
"type": "wikibase-entityid"
}
},
"type": "statement",
"id": question.claim_id,
"rank": correct_rank
})
}
make_edit_call(params)
if question.type == 'P180':
if contrib_request['status'] == 'false':
params = {
'action': 'wbremoveclaims',
'format': 'json',
'claim': question.claim_id
}
make_edit_call(params)

db_set_or_update_user_setting(
session['user_id'],
'contrib_since_test',
Expand Down
68 changes: 62 additions & 6 deletions mdvt/contribute/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
TestContribution, TestQuestion)
from mdvt.database.util import db_get_existing_entry

from requests_oauthlib import OAuth1

qualifiers = [
'P2677',
Expand Down Expand Up @@ -208,6 +209,7 @@ def add_question_if_not_exist(question_type, page_id, depict_id,


# TODO: solve 414 or use id instead of titles
# TODO: lower cyclomatic complexity, split into functions
def get_questions(question_type, filter_type, filter_value, continue_key=None):
true_count = 0
false_count = 0
Expand All @@ -218,7 +220,7 @@ def get_questions(question_type, filter_type, filter_value, continue_key=None):
.all())
for question_id in question_ids:
question = Question.query.filter_by(id=question_id.question_id).first()
if question_type and question.type != question_type:
if question_type and question.type != question_type or question.hidden:
continue
true_count = (Contribution.query
.filter(Contribution.question_id == question.id)
Expand All @@ -243,14 +245,24 @@ def get_questions(question_type, filter_type, filter_value, continue_key=None):
}
).json()

# TODO: handle case where user got the question but another user
# caused it to be deleted

entity = (requests.get(
config['COMMONS_API_URI'],
params={
'action': 'wbgetclaims',
'format': 'json',
'claim': question.claim_id,
}
).json()['claims']['P180'][0])
).json()['claims'])

if 'P180' not in entity:
question.hidden = True
db.session.commit()
continue

entity = entity['P180'][0]

print(entity)

Expand All @@ -262,6 +274,9 @@ def get_questions(question_type, filter_type, filter_value, continue_key=None):
qualifier_value = (entity['qualifiers'][question.type]
[0]['datavalue']['value'])
question.qualifier_value = qualifier_value
if question.type == 'rank':
qualifier_value = entity['rank']
question.qualifier_value = qualifier_value
else:
qualifier_value = None

Expand Down Expand Up @@ -332,16 +347,25 @@ def get_questions(question_type, filter_type, filter_value, continue_key=None):

if type(statements) is dict:
for depict in statements['P180']:
add_question_if_not_exist('P180', entity['pageid'], depict['id'], filter_type, filter_value)
add_question_if_not_exist('rank', entity['pageid'], depict['id'], filter_type, filter_value)
add_question_if_not_exist('P180', entity['pageid'],
depict['id'], filter_type,
filter_value)
add_question_if_not_exist('rank', entity['pageid'],
depict['id'], filter_type,
filter_value)
if 'qualifiers' in depict:
for qualifier in qualifiers:
if qualifier in depict['qualifiers']:
add_question_if_not_exist(qualifier, entity['pageid'], depict['id'], filter_type, filter_value)
add_question_if_not_exist(qualifier,
entity['pageid'],
depict['id'],
filter_type,
filter_value)
except KeyError:
continue

return get_questions(question_type, filter_type, filter_value, continue_key)
return get_questions(question_type, filter_type, filter_value,
continue_key)


def get_test_questions():
Expand Down Expand Up @@ -443,3 +467,35 @@ def get_file_depicts(file_name):
return (depict_id, depict_label, depict_description, claim_id)
except KeyError:
return None


def make_edit_call(params):
auth = OAuth1(config['OAUTH_TOKEN'],
config['OAUTH_SECRET'],
session.get('access_token')['key'],
session.get('access_token')['secret'])

token = requests.get(
config['COMMONS_API_URI'],
params={
'action': 'query',
'meta': 'tokens',
'format': 'json',
},
auth=auth
)

token = token.json()['query']['tokens']['csrftoken']

params['token'] = token

print('Edit to make:')
print(params)

response = requests.post(
config['COMMONS_API_URI'],
data=params,
auth=auth
)

print(response.json())
6 changes: 4 additions & 2 deletions mdvt/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class Question(db.Model):
claim_id = db.Column(db.String(255), nullable=False)
depict_value = db.Column(db.String(255), nullable=True)
qualifier_value = db.Column(db.String(1023), nullable=True)
hidden = db.Column(db.Boolean, nullable=False, default=False)

def __repr__(self):
return '<Question {} {} {} {}>'.format(
self.id, self.page_id, self.type, self.claim_id)
return '<Question {} {} {} {} {} {} {}>'.format(
self.id, self.page_id, self.type, self.claim_id,
self.depict_value, self.qualifier_value, self.hidden)


class TestQuestion(db.Model):
Expand Down
2 changes: 1 addition & 1 deletion mdvt/templates/contribute/contribute.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<h1 class="h2 text-center" id="statement"></h1>
<div class="row mt-3">
<div class="col-sm-4 mt-1">
<button type="button" id="true-btn" class="btn btn-success btn-lg btn-block">{{ _('Treu') }}</button>
<button type="button" id="true-btn" class="btn btn-success btn-lg btn-block">{{ _('True') }}</button>
</div>
<div class="col-sm-4 mt-1">
<button type="button" id="false-btn" class="btn btn-danger btn-lg btn-block">{{ _('False') }}</button>
Expand Down

0 comments on commit f4741f9

Please sign in to comment.