Skip to content

Commit d605f82

Browse files
author
vasgat
committed
add published filter on get_answers, get_relationship_answers, get_answers_by_metric_id, get_relationship_answers_by_metric_id
1 parent f00eb23 commit d605f82

File tree

1 file changed

+134
-11
lines changed

1 file changed

+134
-11
lines changed

wikirate4py/api.py

+134-11
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ def get_company(self, identifier) -> Company:
149149
if isinstance(identifier, int):
150150
return self.get("/~{0}.json".format(identifier))
151151
else:
152-
return self.get("/{0}.json".format(identifier.replace(" ", "_")))
152+
return self.get("/{0}.json".format(
153+
identifier.replace(',', ' ').replace('.', ' ').replace('/', ' ').replace('-', ' ').strip().replace(" ",
154+
"_")))
153155

154156
@objectify(CompanyItem, list=True)
155157
def get_companies(self, **kwargs):
@@ -191,7 +193,9 @@ def get_topic(self, identifier):
191193
if isinstance(identifier, int):
192194
return self.get("/~{0}.json".format(identifier))
193195
else:
194-
return self.get("/{0}.json".format(identifier.replace(" ", "_")))
196+
return self.get("/{0}.json".format(
197+
identifier.replace(',', ' ').replace('.', ' ').replace('/', ' ').replace('-', ' ').strip().replace(" ",
198+
"_")))
195199

196200
@objectify(TopicItem, True)
197201
def get_topics(self, **kwargs):
@@ -280,7 +284,9 @@ def get_research_group(self, identifier):
280284
if isinstance(identifier, int):
281285
return self.get("/~{0}.json".format(identifier))
282286
else:
283-
return self.get("/{0}.json".format(identifier.replace(" ", "_")))
287+
return self.get("/{0}.json".format(
288+
identifier.replace(',', ' ').replace('.', ' ').replace('/', ' ').replace('-', ' ').strip().replace(" ",
289+
"_")))
284290

285291
@objectify(ResearchGroupItem, list=True)
286292
def get_research_groups(self, **kwargs):
@@ -319,7 +325,9 @@ def get_company_group(self, identifier):
319325
if isinstance(identifier, int):
320326
return self.get("/~{0}.json".format(identifier))
321327
else:
322-
return self.get("/{0}.json".format(identifier.replace(" ", "_")))
328+
return self.get("/{0}.json".format(
329+
identifier.replace(',', ' ').replace('.', ' ').replace('/', ' ').replace('-', ' ').strip().replace(" ",
330+
"_")))
323331

324332
@objectify(CompanyGroupItem, True)
325333
def get_company_groups(self, **kwargs):
@@ -359,7 +367,9 @@ def get_source(self, identifier):
359367
if isinstance(identifier, int):
360368
return self.get("/~{0}.json".format(identifier))
361369
else:
362-
return self.get("/{0}.json".format(identifier.replace(" ", "_")))
370+
return self.get("/{0}.json".format(
371+
identifier.replace(',', ' ').replace('.', ' ').replace('/', ' ').replace('-', ' ').strip().replace(" ",
372+
"_")))
363373

364374
@objectify(SourceItem, True)
365375
def get_sources(self, **kwargs):
@@ -473,7 +483,8 @@ def get_answers_by_metric_id(self, metric_id, **kwargs):
473483
return self.get("/~{0}+Answer.json".format(metric_id), endpoint_params=('limit', 'offset'),
474484
filters=(
475485
'year', 'status', 'company_group', 'country', 'value', 'value_from', 'value_to', 'updated',
476-
'updater', 'outliers', 'source', 'verification', 'project', 'bookmark'), **kwargs)
486+
'updater', 'outliers', 'source', 'verification', 'project', 'bookmark', 'published'),
487+
**kwargs)
477488

478489
@objectify(AnswerItem, True)
479490
def get_answers(self, metric_name, metric_designer, **kwargs):
@@ -545,18 +556,23 @@ def get_answers(self, metric_name, metric_designer, **kwargs):
545556
bookmark
546557
- `bookmark`, restrict to answers you have bookmarked
547558
- `nobookmark`, restrict to answers you have not bookmarked
559+
published
560+
- `true`, returns only published answers (default mode)
561+
- `false`, returns only unpublished answers
562+
- `all`, returns all published and unpublished answers
563+
548564
549565
Returns
550566
-------
551567
:py:class:`List`\[:class:`~wikirate4py.models.AnswerItem`]
552568
553569
"""
554570
return self.get(
555-
"/~{0}+{1}+Answer.json".format(metric_designer.replace(" ", "_"), metric_name.replace(" ", "_")),
571+
"/{0}+{1}+Answer.json".format(metric_designer.replace(" ", "_"), metric_name.replace(" ", "_")),
556572
endpoint_params=('limit', 'offset'),
557573
filters=(
558574
'year', 'status', 'company_group', 'country', 'value', 'value_from', 'value_to', 'updated',
559-
'updater', 'outliers', 'source', 'verification', 'project', 'bookmark'), **kwargs)
575+
'updater', 'outliers', 'source', 'verification', 'project', 'bookmark', 'published'), **kwargs)
560576

561577
@objectify(RelationshipAnswer)
562578
def get_relationship_answer(self, id):
@@ -644,6 +660,11 @@ def get_relationship_answers_by_metric_id(self, metric_id, **kwargs):
644660
- `bookmark`, restrict to relationship answers you have bookmarked
645661
- `nobookmark`, restrict to relationship answers you have not bookmarked
646662
663+
published
664+
- `true`, returns only published answers (default mode)
665+
- `false`, returns only unpublished answers
666+
- `all`, returns all published and unpublished answers
667+
647668
Returns
648669
-------
649670
:py:class:`List`\[:class:`~wikirate4py.models.RelationshipAnswerItem`]
@@ -652,7 +673,7 @@ def get_relationship_answers_by_metric_id(self, metric_id, **kwargs):
652673
return self.get("/~{0}+Relationship_Answer.json".format(metric_id),
653674
endpoint_params=('limit', 'offset'), filters=(
654675
'year', 'status', 'company_group', 'country', 'value', 'value_from', 'value_to', 'updated',
655-
'updater', 'outliers', 'source', 'verification', 'project', 'bookmark'), **kwargs)
676+
'updater', 'outliers', 'source', 'verification', 'project', 'bookmark', 'published'), **kwargs)
656677

657678
@objectify(RelationshipAnswerItem, True)
658679
def get_relationship_answers(self, metric_name, metric_designer, **kwargs):
@@ -722,6 +743,11 @@ def get_relationship_answers(self, metric_name, metric_designer, **kwargs):
722743
- `bookmark`, restrict to relationship answers you have bookmarked
723744
- `nobookmark`, restrict to relationship answers you have not bookmarked
724745
746+
published
747+
- `true`, returns only published answers (default mode)
748+
- `false`, returns only unpublished answers
749+
- `all`, returns all published and unpublished answers
750+
725751
Returns
726752
-------
727753
:py:class:`List`\[:class:`~wikirate4py.models.RelationshipAnswerItem`]
@@ -732,7 +758,7 @@ def get_relationship_answers(self, metric_name, metric_designer, **kwargs):
732758
metric_name.replace(" ", "_")),
733759
endpoint_params=('limit', 'offset'), filters=(
734760
'year', 'status', 'company_group', 'country', 'value', 'value_from', 'value_to', 'updated',
735-
'updater', 'outliers', 'source', 'verification', 'project', 'bookmark'), **kwargs)
761+
'updater', 'outliers', 'source', 'verification', 'project', 'bookmark', 'published'), **kwargs)
736762

737763
@objectify(Project)
738764
def get_project(self, identifier):
@@ -752,7 +778,9 @@ def get_project(self, identifier):
752778
if isinstance(identifier, int):
753779
return self.get("/~{0}.json".format(identifier))
754780
else:
755-
return self.get("/{0}.json".format(identifier.replace(" ", "_")))
781+
return self.get("/{0}.json".format(
782+
identifier.replace(',', ' ').replace('.', ' ').replace('/', ' ').replace('-', ' ').strip().replace(" ",
783+
"_")))
756784

757785
@objectify(ProjectItem, True)
758786
def get_projects(self, **kwargs):
@@ -944,6 +972,86 @@ def add_company(self, name, headquarters, **kwargs):
944972

945973
return self.post("/card/create", params)
946974

975+
@objectify(Company)
976+
def add_company(self, name, headquarters, **kwargs):
977+
"""add_company(name, headquarters, *, oar_id, open_corporates)
978+
979+
Creates and Returns a company given the company name and headquarters
980+
981+
Parameters
982+
----------
983+
name
984+
company name
985+
headquarters
986+
name of the region the headquarters of the company is located
987+
wikipedia
988+
company's wikipedia page url
989+
oar_id
990+
company's identifier on https://openapparel.org
991+
open_corporates
992+
company's identifier on https://opencorporates.com/
993+
994+
Returns
995+
-------
996+
:py:class:`~wikirate4py.models.Company`
997+
998+
"""
999+
1000+
if name is None or headquarters is None:
1001+
raise WikiRate4PyException(
1002+
'A WikiRate company is defined by a name and headquarters, please be sure you '
1003+
'have defined both while trying to create a new company')
1004+
optional_params = ('oar_id', 'wikipedia', 'open_corporates')
1005+
params = {
1006+
"card[type]": "Company",
1007+
"card[name]": name,
1008+
"card[subcards][+headquarters]": headquarters,
1009+
"format": "json",
1010+
"success[format]": "json"
1011+
}
1012+
1013+
for k, arg in kwargs.items():
1014+
if arg is None:
1015+
continue
1016+
if k not in optional_params:
1017+
log.warning(f'Unexpected parameter: {k}')
1018+
else:
1019+
params['card[subcards][+' + k + ']'] = str(arg)
1020+
log.debug("PARAMS: %r", params)
1021+
if 'open_corporates' not in kwargs:
1022+
params['card[skip]'] = "update_oc_mapping_due_to_headquarters_entry"
1023+
1024+
return self.post("/card/create", params)
1025+
1026+
@objectify(RegionItem, True)
1027+
def update_headquarters(self, identifier, headquarters):
1028+
params = {
1029+
"card[content]": headquarters,
1030+
"format": "json",
1031+
"success[format]": "json"
1032+
}
1033+
if isinstance(identifier, int):
1034+
return self.post("/update/~{0}".format(identifier) + '+:headquarters', params)
1035+
else:
1036+
return self.post("/update/{0}".format(
1037+
identifier.replace(',', ' ').replace('.', ' ').replace('/', ' ').replace('-', ' ').strip().replace(" ",
1038+
"_")) + '+:headquarters',
1039+
params)
1040+
1041+
def update_oc_company_number(self, identifier, company_number):
1042+
params = {
1043+
"card[content]": company_number,
1044+
"format": "json",
1045+
"success[format]": "json"
1046+
}
1047+
if isinstance(identifier, int):
1048+
return self.post("/update/~{0}".format(identifier) + '+OpenCorporates', params)
1049+
else:
1050+
return self.post("/update/{0}".format(
1051+
identifier.replace(',', ' ').replace('.', ' ').replace('/', ' ').replace('-', ' ').strip().replace(" ",
1052+
"_")) + '+OpenCorporates',
1053+
params)
1054+
9471055
@objectify(Answer)
9481056
def add_research_metric_answer(self, **kwargs):
9491057
"""add_research_metric_answer(metric_designer, metric_name, company, year, value, source, *, comment)
@@ -1304,3 +1412,18 @@ def delete_wikirate_entity(self, id):
13041412
Deletes a WikiRate entity based on the given numeric identifier
13051413
"""
13061414
return self.delete("/~{0}".format(id))
1415+
1416+
def add_companies_to_group(self, group_id, list=[]):
1417+
ids = ""
1418+
for item in list:
1419+
ids += '~[[' + item + ']]\n'
1420+
print(ids)
1421+
params = {
1422+
"card[type]": "List",
1423+
"card[name]": '~' + group_id + '+' + 'Company',
1424+
"card[content]": ids,
1425+
"format": "json",
1426+
"success[format]": "json"
1427+
}
1428+
1429+
return self.post("/card/update", params)

0 commit comments

Comments
 (0)