Skip to content

Commit

Permalink
Merge pull request #896 from bcgov/revert-816-test-AH-2889
Browse files Browse the repository at this point in the history
Revert "Ticket 2889. Adjust pageflag logic to apply appropriate pageflag based on redactions present"
  • Loading branch information
Aman-Hundal authored Mar 22, 2024
2 parents 2214446 + cadb24f commit 83a170d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 143 deletions.
26 changes: 0 additions & 26 deletions api/reviewer_api/models/Annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,31 +537,6 @@ def getredactionsbydocumentpages(cls, _documentid, _pages, redactionlayerid):
logging.error(ex)
finally:
db.session.close()

@classmethod
def getredactionannotationsbydocumentpages(cls, docpagemapping, redactionlayerid):
try:
annotation_schema = AnnotationSchema(many=True)
query = (
db.session.query(Annotation.documentid, Annotation.pagenumber, func.array_agg(Annotation.annotation).label('annotations'))
.filter(
and_(
Annotation.annotation.ilike("%<redact %"),
Annotation.redactionlayerid == redactionlayerid,
Annotation.isactive == True
),
or_(
and_(Annotation.documentid == docid, Annotation.pagenumber == pagenumber)
for docid, pagenumber in docpagemapping
))
.group_by(Annotation.documentid, Annotation.pagenumber)
.all()
)
return annotation_schema.dump(query)
except Exception as ex:
logging.error(ex)
finally:
db.session.close()


class AnnotationSchema(ma.Schema):
Expand All @@ -572,7 +547,6 @@ class Meta:
"documentid",
"documentversion",
"annotation",
"annotations",
"pagenumber",
"redactionlayerid",
"redactionlayer.redactionlayerid",
Expand Down
35 changes: 0 additions & 35 deletions api/reviewer_api/services/documentpageflagservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from reviewer_api.services.redactionlayerservice import redactionlayerservice
from reviewer_api.models.default_method_result import DefaultMethodResult
from datetime import datetime
from reviewer_api.models.Pageflags import Pageflag


class documentpageflagservice:
Expand Down Expand Up @@ -173,26 +172,6 @@ def updatepageflags(
pageflags, deldocpagesmapping, requestid, userinfo, redactionlayerid
)

def bulkupdatepageflags(self, requestid, data, redactionlayerid, userinfo):
docids = [docflagobj["docid"] for docflagobj in data]
previousdocpageflags = self.getdocumentpageflagsbydocids(requestid, redactionlayerid, docids)
# loop through data and adjust docobj in data (updated page flags to apply to page / doc id) with previous docpageflag data (data from DocumentPageFlag Table)
for docflagobj in data:
for prevdocflagobj in previousdocpageflags:
if (docflagobj['docid'] == prevdocflagobj['documentid']):
docflagobj['version'] = prevdocflagobj['documentversion']
docflagobj["attributes"] = prevdocflagobj["attributes"]
docflagobj['pageflag'] = self.__findandreplacepageflags(docflagobj['pageflag'], prevdocflagobj['pageflag'])
DocumentPageflag.savepageflag(
requestid,
docflagobj["docid"],
docflagobj["version"],
json.dumps(docflagobj['pageflag']),
json.dumps(userinfo),
redactionlayerid,
json.dumps(docflagobj["attributes"]),
)

def __filterandsavepageflags(
self,
pageflags,
Expand Down Expand Up @@ -283,17 +262,3 @@ def __isbookmark(self, data):
if data["flagid"] == 8:
return True
return False

def __findandreplacepageflags(self, newpageflags, previouspageflags):
updatedpageflags = []
dictlookup = {item["page"]: item["flagid"] for item in newpageflags}
for pageflag in previouspageflags:
page = pageflag['page']
if page in dictlookup:
updatedpageflags.append({
"page": page,
"flagid": dictlookup.get(page)
})
else:
updatedpageflags.append(pageflag)
return [pageflag for pageflag in updatedpageflags if pageflag['flagid'] is not None]
113 changes: 32 additions & 81 deletions api/reviewer_api/services/radactionservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

from reviewer_api.utils.util import to_json
from datetime import datetime

from xml.dom.minidom import parseString
import json

class redactionservice:
Expand Down Expand Up @@ -55,7 +53,6 @@ def copyannotation(self, ministryrequestid, targetlayer):

def saveannotation(self, annotationschema, userinfo):
result = annotationservice().saveannotation(annotationschema, userinfo)
#pageflag logic
if (
result.success == True
and "pageflags" in annotationschema
Expand All @@ -69,10 +66,9 @@ def saveannotation(self, annotationschema, userinfo):
"foiministryrequestid"
]
if foiministryrequestid:
bulkaddpageflagdata = self.__preparebulkaddpageflagdata(foiministryrequestid, annotationschema["pageflags"], annotationschema['redactionlayerid'])
documentpageflagservice().bulksavepageflag(
foiministryrequestid,
bulkaddpageflagdata,
annotationschema["pageflags"],
userinfo,
)
return result
Expand Down Expand Up @@ -106,58 +102,60 @@ def deactivateredaction(
annotationnames, redactionlayerid, userinfo
)
### Remove/Update pageflags start here ###
# totaldocumentpagesmapping docid and pages mapping for annotations deleted
# totaldocumentpagesmapping docid and pages mapping
inputdocpagesmapping = self.__getdocumentpagesmapping(
annotationschema["annotations"]
)
# find all active redactions for documents related to removed annotation. DB call to get the (document, pages, redaction) with redactions in it.
documentactiveredactions = self.__getactivedocpagesmapping(
# skip the pages as it has redactions in it. DB call to get the (document, pages) with redactions in it.
skipdocpagesmapping = self.__getskipdocpagesmapping(
inputdocpagesmapping, redactionlayerid
)
# create docmappings for pages with no active redactions remaining (to delete all page flags for docpageflag)
# pages not having redactions
deldocpagesmapping = self.__getdeldocpagesmapping(
inputdocpagesmapping, documentactiveredactions
)
# create docmappings for pages with active redactions remaining (to update page flags for docpageflag)
pageswithactiveredacitons = {
(item["documentid"], item["pagenumber"]) for item in documentactiveredactions
}
docpageredcations = Annotation.getredactionannotationsbydocumentpages(pageswithactiveredacitons, redactionlayerid) if len(pageswithactiveredacitons) > 0 else []
# update page flags for pages with redactions remaining (pageswithactiveredacitons) + pages not having any redactions remaining (deldocpagemapping)
bulkupdateflagdata = self.__preparebulkupdatepageflagdata(pageswithactiveredacitons, docpageredcations, deldocpagesmapping)
documentpageflagservice().bulkupdatepageflags(
requestid,
bulkupdateflagdata,
redactionlayerid,
userinfo
inputdocpagesmapping, skipdocpagesmapping
)
if deldocpagesmapping:
documentpageflagservice().updatepageflags(
requestid,
deldocpagesmapping,
redactionlayerid,
userinfo,
)
### Remove/Update pageflags end here ###
return result

def __getactivedocpagesmapping(self, _documentpagesmapping, _redactionlayerid):
activedata = []
def __getskipdocpagesmapping(self, _documentpagesmapping, _redactionlayerid):
skipdata = []
# gets the documentid and pages to skip based on documentid and pages
for item in _documentpagesmapping:
docid = item["docid"]
pages = item["pages"]
activedata.extend(
skipdata.extend(
Annotation.getredactionsbydocumentpages(docid, pages, _redactionlayerid)
)
return activedata
return skipdata

def __getdeldocpagesmapping(self, inputdocpagesmapping, skipdocpagesmapping):
deldocpagesmapping = set()
# item["pagenumber"] + 1 is because the page in pageflags starts with 1
skip_combinations = {
(item["documentid"], item["pagenumber"])
for item in skipdocpagesmapping
if len(skipdocpagesmapping) > 0
}

# Filter inputdocpagesmapping to obtain deletedocumentpagesmapping
for item in inputdocpagesmapping:
docid = item["docid"]
if (len(item['pages']) > 0):
for page in item['pages']:
if ((docid, page) not in skip_combinations):
deldocpagesmapping.add((docid, page))
deldocpagesmapping = [
{
"docid": item["docid"],
"pages": [
page + 1
for page in item["pages"]
if (item["docid"], page) not in skip_combinations
],
}
for item in inputdocpagesmapping
if len(item["pages"]) > 0
]
return deldocpagesmapping

def __getdocumentpagesmapping(self, annotations):
Expand Down Expand Up @@ -239,50 +237,3 @@ def __preparemessageforjobstatus(self, messageschema):
"inputfiles": messageschema["attributes"],
}
return __message

def __preparebulkaddpageflagdata(self, requestid, annot_pageflags, redactionlayerid):
docids = [doc['documentid'] for doc in annot_pageflags['documentpageflags']]
docpreviousflags = documentpageflagservice().getdocumentpageflagsbydocids(requestid, redactionlayerid, docids)
for i in range(len(annot_pageflags['documentpageflags'])):
doc = annot_pageflags['documentpageflags'][i]
for j in range(len(doc['pageflags'])):
pageflag = doc['pageflags'][j]
#loop through previous docplageflag data, find assoacited docpageflags using docid and see if withheld in full page flag (flagid 3) exists for the new annotations page
for docpageobj in docpreviousflags:
if (doc['documentid'] == docpageobj['documentid'] and {"page": pageflag['page'], "flagid": 3} in docpageobj['pageflag']):
annot_pageflags['documentpageflags'][i]['pageflags'][j] = {"page": pageflag['page'], "flagid": 3}
return annot_pageflags

def __preparebulkupdatepageflagdata(self, pageswithactiveredacitons, docpageredcations, deldocpagesmapping):
# page + 1 because in DB pages start at 1
bulkupdatedata = {}
# loop through set of docpagemappings to delete/remove page flags
for docid, page in deldocpagesmapping:
newpageflag = {"page": page + 1, "flagid": None}
if (docid not in bulkupdatedata):
bulkupdatedata[docid] = {"docid": docid, "pageflag": []}
bulkupdatedata[docid]['pageflag'].append(newpageflag)
# loop through set of pages with redaction remaining in them to update pageflags to partial or withehld in full
for docid, page in pageswithactiveredacitons:
for docobj in docpageredcations:
if (docid == docobj['documentid'] and page == docobj['pagenumber']):
redactions = docobj['annotations']
flagid = self.__createflagidfromredactions(redactions)
newpageflag = {"page": page + 1, "flagid": flagid}
if (docid not in bulkupdatedata):
bulkupdatedata[docid] = {"docid": docid, "pageflag": []}
bulkupdatedata[docid]['pageflag'].append(newpageflag)
return list(bulkupdatedata.values())

def __createflagidfromredactions(self, redactions):
# conditional to check if fullpage xml data exists in a pages redaction (if it does -> flagid of 3 is applied else flagid of)
if (any(
'trn-redaction-type' in json.loads(parseString(redaction).getElementsByTagName("trn-custom-data")[0].getAttribute("bytes"))
and json.loads(parseString(redaction).getElementsByTagName("trn-custom-data")[0].getAttribute("bytes"))['trn-redaction-type'] == 'fullPage'
for redaction in redactions
)):
flagid = 3
else:
flagid = 1
return flagid

2 changes: 1 addition & 1 deletion web/src/components/FOI/Home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function Home() {
(data) => {
let redline = data.find((l) => l.name === "Redline");
let oipc = data.find((l) => l.name === "OIPC");
let currentLayer = validoipcreviewlayer && oipc.count > 0 ? oipc : redline;
let currentLayer = validoipcreviewlayer && oipc.count > 0 ? oipc : redline;
store.dispatch(setCurrentLayer(currentLayer));
},
(error) => console.log(error)
Expand Down

0 comments on commit 83a170d

Please sign in to comment.