Skip to content

Commit 3282883

Browse files
authored
Merge pull request #149 from WikiMovimentoBrasil/2025-01-fix-globe-coordinates-by-wiki
fix: units by wiki / remove aliases
2 parents 12f00e8 + 03deb42 commit 3282883

File tree

8 files changed

+68
-33
lines changed

8 files changed

+68
-33
lines changed

src/core/models.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ def save_batch_and_preview_commands(self):
224224
batch_command.batch = self
225225
batch_command.save()
226226

227+
def wikibase_url(self):
228+
"""
229+
Returns the wikibase url of this batch.
230+
231+
In the future this could be a field, so that batches targeting
232+
different wikibases (like wikidata and test.wikidata) are
233+
possible in the same server.
234+
"""
235+
return settings.BASE_REST_URL.replace("https://", "http://").split("/w/rest.php")[0]
236+
227237

228238
class BatchCommand(models.Model):
229239
"""
@@ -477,10 +487,10 @@ def parser_value_to_api_value(self, parser_value):
477487
def statement_api_value(self):
478488
value = self.json["value"]
479489
if value["type"] == "quantity" and value["value"]["unit"] != "1":
480-
# TODO: the unit is an entity and we need to put the
481-
# full entity URL... so we need the client
482-
# to process the URL
483-
raise NotImplementedError()
490+
base = self.batch.wikibase_url()
491+
unit_id = value["value"]["unit"]
492+
full_unit = f"{base}/entity/Q{unit_id}"
493+
value["value"]["unit"] = full_unit
484494
return self.parser_value_to_api_value(value)
485495

486496
def update_statement(self, st):
@@ -651,12 +661,17 @@ def edit_summary(self):
651661
652662
It joins the user supplied summary with
653663
the identification necessary for EditGroups.
664+
665+
Also joins the summary from the previous combined commands.
654666
"""
667+
summaries = [self.user_summary]
668+
summaries.extend([c.user_summary for c in getattr(self, "previous_commands", [])])
669+
combined = " | ".join([s for s in summaries[::-1] if bool(s)])
655670
editgroups = self.editgroups_summary()
656671
if editgroups:
657-
return f"{editgroups}: {self.user_summary}" if self.user_summary else editgroups
672+
return f"{editgroups}: {combined}" if combined else editgroups
658673
else:
659-
return self.user_summary if self.user_summary else ""
674+
return combined
660675

661676
def editgroups_summary(self):
662677
"""
@@ -685,6 +700,7 @@ def is_entity_json_patch(self):
685700
self.Operation.SET_LABEL,
686701
self.Operation.SET_DESCRIPTION,
687702
self.Operation.SET_SITELINK,
703+
self.Operation.REMOVE_ALIAS,
688704
self.Operation.REMOVE_LABEL,
689705
self.Operation.REMOVE_DESCRIPTION,
690706
self.Operation.REMOVE_SITELINK,
@@ -775,13 +791,10 @@ def update_entity_json(self, entity: dict):
775791
self._update_entity_statements(entity)
776792
elif self.operation == self.Operation.REMOVE_STATEMENT_BY_VALUE:
777793
self._remove_entity_statement(entity)
794+
elif self.operation in (self.Operation.ADD_ALIAS, self.Operation.REMOVE_ALIAS):
795+
self._update_entity_aliases(entity)
778796
elif self.operation in (self.Operation.REMOVE_QUALIFIER, self.Operation.REMOVE_REFERENCE):
779797
self._remove_qualifier_or_reference(entity)
780-
elif self.operation == self.Operation.ADD_ALIAS:
781-
entity["aliases"].setdefault(self.language, [])
782-
for alias in self.value_value:
783-
if alias not in entity["aliases"][self.language]:
784-
entity["aliases"][self.language].append(alias)
785798
elif self.operation == self.Operation.SET_SITELINK:
786799
entity["sitelinks"][self.sitelink] = {"title": self.value_value}
787800
elif self.operation in (self.Operation.SET_LABEL, self.Operation.SET_DESCRIPTION):
@@ -840,6 +853,24 @@ def _remove_entity_statement(self, entity: dict):
840853
return entity["statements"][self.prop].pop(i)
841854
raise NoStatementsWithThatValue(self.entity_id(), self.prop, self.statement_api_value)
842855

856+
def _update_entity_aliases(self, entity: dict):
857+
"""
858+
Update the entity's aliases, adding or removing.
859+
"""
860+
entity["aliases"].setdefault(self.language, [])
861+
aliases = entity["aliases"][self.language]
862+
if self.operation == self.Operation.ADD_ALIAS:
863+
for alias in self.value_value:
864+
if alias not in aliases:
865+
aliases.append(alias)
866+
elif self.operation == self.Operation.REMOVE_ALIAS:
867+
new = [a for a in aliases if a not in self.value_value]
868+
if len(new) > 0:
869+
entity["aliases"][self.language] = new
870+
else:
871+
# It is not possible to leave a language with 0 aliases
872+
entity["aliases"].pop(self.language)
873+
843874
def entity_patch(self, client: Client):
844875
"""
845876
Calculates the entity json patch to send to the API.
@@ -851,13 +882,9 @@ def entity_patch(self, client: Client):
851882
TODO: maybe cache that original as well to not make
852883
two requests?
853884
"""
854-
logger.debug(f"[{self}] BEFORE ORIGINAL...")
855885
original = self.get_original_entity_json(client)
856-
logger.debug(f"[{self}] BEFORE PREVIOUS...")
857886
entity = self.get_previous_entity_json(client)
858-
logger.debug(f"[{self}] AFTER BOTH...")
859887
self.update_entity_json(entity)
860-
logger.debug(f"[{self}] AFTER UPDATE...")
861888
return jsonpatch.JsonPatch.from_diff(original, entity).patch
862889

863890
def api_payload(self, client: Client):
@@ -893,7 +920,7 @@ def send_to_api(self, client: Client) -> dict:
893920
is not implemented.
894921
"""
895922
match self.operation:
896-
case self.Operation.CREATE_PROPERTY | self.Operation.REMOVE_ALIAS:
923+
case self.Operation.CREATE_PROPERTY:
897924
raise NotImplementedError()
898925
case _:
899926
return self.send_basic(client)

src/core/parsers/base.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,13 @@ def parse_value_location(self, v):
301301
return {
302302
"type": "globecoordinate",
303303
"value": {
304-
"latitude": gps_match.group(1),
305-
"longitude": gps_match.group(2),
306-
"precision": "0.000001",
304+
"latitude": float(gps_match.group(1)),
305+
"longitude": float(gps_match.group(2)),
306+
# original quickstatements precision
307+
# is always fixed as well
308+
"precision": 0.000001,
309+
# Even in test.wikidata.org the globe used
310+
# is wikidata's Q2, so we can keep it fixed
307311
"globe": "http://www.wikidata.org/entity/Q2",
308312
},
309313
}

src/core/parsers/v1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def parse_statement(self, elements, first_command):
134134
if not valias or valias["type"] != "string":
135135
raise ParserException("alias must be a string instance")
136136
aliases.append(valias["value"])
137-
data = {"action": "add", "what": "alias", "language": lang, "item": entity, "value": {"type": "aliases", "value": aliases}}
137+
data = {"action": action, "what": "alias", "language": lang, "item": entity, "value": {"type": "aliases", "value": aliases}}
138138

139139
elif llen == 3 and elements[1][0] in ["L", "D", "S"]:
140140
# We are adding / removing a LABEL, ALIAS, DESCRIPTION or SITELINK to our property

src/core/tests/test_base_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ def test_parse_value_location(self):
302302
ret = {
303303
"type": "globecoordinate",
304304
"value": {
305-
"latitude": "43.26193",
306-
"longitude": "10.92708",
307-
"precision": "0.000001",
305+
"latitude": 43.26193,
306+
"longitude": 10.92708,
307+
"precision": 0.000001,
308308
"globe": "http://www.wikidata.org/entity/Q2",
309309
},
310310
}

src/core/tests/test_csv_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ def test_parse_coordinates(self):
275275
"type": "globecoordinate",
276276
"value": {
277277
"globe": "http://www.wikidata.org/entity/Q2",
278-
"latitude": "43.26193",
279-
"longitude": "10.92708",
280-
"precision": "0.000001",
278+
"latitude": 43.26193,
279+
"longitude": 10.92708,
280+
"precision": 0.000001,
281281
},
282282
},
283283
"what": "statement",

src/core/tests/test_v1_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ def test_v1_add_location(self):
357357
"value": {
358358
"type": "globecoordinate",
359359
"value": {
360-
"latitude": "43.26193",
361-
"longitude": "10.92708",
362-
"precision": "0.000001",
360+
"latitude": 43.26193,
361+
"longitude": 10.92708,
362+
"precision": 0.000001,
363363
"globe": "http://www.wikidata.org/entity/Q2",
364364
},
365365
},

src/integration/tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ def test_Q238107(self):
4242
Q238107|Len|"QuickStatements 3.0 test item"
4343
Q238107|Den|"A test item for the QuickStatements 3.0 project"
4444
Q238107|Aen|"A test item for QuickStatements"
45+
Q238107|Aen|"Removable alias"
4546
Q238107|Dpt|"Um item de teste do projeto QuickStatements 3.0"
4647
Q238107|Lpt|"Teste - rótulo a ser removido posteriormente"
4748
Q238107|Lpt|""
49+
-Q238107|Aen|"Removable alias"
4850
Q238107|Smetawiki|"QuickStatements 3.0"
4951
-Q238107|P65|42
52+
-Q238107|P372|1.5U71737
5053
-Q238107|P31|somevalue
5154
-Q238107|P18|+2001-01-15T00:00:00Z/11
5255
-Q238107|P196|novalue
5356
Q238107|P65|42|R+|P65|84|P84267|-5|P93|"https://toberemoved.org/"
57+
Q238107|P372|1.5U71737
5458
REMOVE_QUAL|Q238107|P65|42|P93|"https://toberemoved.org/"
5559
Q238107|P31|somevalue|P18|+2025-01-15T00:00:00Z/11|S93|"https://kernel.org/"|S84267|42|!S93|"https://www.mediawiki.org/"|S74|+1980-10-21T00:00:00Z/11
5660
Q238107|P18|+2001-01-15T00:00:00Z/11|R0|S65|999|!S74|+2012-12-21T00:00:00Z/11

src/web/templates/batch_commands.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@
8888

8989
{% if page.has_previous %}
9090
<span class="pagination prev-page">
91-
<a hx-get="{{base_url}}?page=1{% if only_errors %}&show_errors=1{% endif %}"
91+
<a href="#" hx-get="{{base_url}}?page=1{% if only_errors %}&show_errors=1{% endif %}"
9292
hx-target="#batchCommandsDiv"
9393
hx-swap="innerHTML">
9494
<< {% translate "FIRST" %}
9595
</a>
9696
</span>
9797

9898
<span class="pagination prev-page">
99-
<a hx-get="{{base_url}}?page={{page.previous_page_number}}{% if only_errors %}&show_errors=1{% endif %}"
99+
<a href="#" hx-get="{{base_url}}?page={{page.previous_page_number}}{% if only_errors %}&show_errors=1{% endif %}"
100100
hx-target="#batchCommandsDiv"
101101
hx-swap="innerHTML">
102102
< {% translate "PREVIOUS" %}
@@ -118,15 +118,15 @@
118118

119119
{% if page.has_next %}
120120
<span class="pagination next-page">
121-
<a hx-get="{{base_url}}?page={{page.next_page_number}}{% if only_errors %}&show_errors=1{% endif %}"
121+
<a href="#" hx-get="{{base_url}}?page={{page.next_page_number}}{% if only_errors %}&show_errors=1{% endif %}"
122122
hx-target="#batchCommandsDiv"
123123
hx-swap="innerHTML">
124124
{% translate "NEXT" %} >
125125
</a>
126126
</span>
127127

128128
<span class="pagination next-page">
129-
<a hx-get="{{base_url}}?page={{page.paginator.num_pages}}{% if only_errors %}&show_errors=1{% endif %}"
129+
<a href="#" hx-get="{{base_url}}?page={{page.paginator.num_pages}}{% if only_errors %}&show_errors=1{% endif %}"
130130
hx-target="#batchCommandsDiv"
131131
hx-swap="innerHTML">
132132
{% translate "LAST" %}>>

0 commit comments

Comments
 (0)