def confirm_account_task(self, comm_pk, link): """Do the confirmation in a task""" comm = FOIACommunication.objects.get(pk=comm_pk) reply = requests.get(link) if reply.status_code == 200: # If there were initial files on the request, # they must be uploaded after confirming the account first_comm = comm.foia.communications.first() if first_comm.files.exists(): try: request_id = self._get_request_id(first_comm) session = requests.Session() self._login(first_comm, session) self._send_documents(first_comm, session, request_id) except PortalError as exc: PortalTask.objects.create( category='u', communication=first_comm, reason='The request was successfully created but ' 'the attachments were not successfully uploaded\n' + exc.args[0]) PortalCommunication.objects.create( communication=comm, sent_datetime=timezone.now(), portal=self.portal, direction='incoming', ) else: ManualPortal.receive_msg( self, comm, reason='Confirmation link failed', )
def _process_msg(self, comm, regex, on_match, error_reason): """Process an incoming message based on a regex match of the content""" pattern = re.compile(regex, re.MULTILINE | re.DOTALL | re.UNICODE) match = pattern.search(comm.communication) if match: on_match(match) else: ManualPortal.receive_msg(self, comm, reason=error_reason)
def document_reply(self, comm): """Process incoming documents""" p_file_available = re.compile( r"There are eFOIA files available for you to download") match = p_file_available.search(comm.communication) if match: portal_task.delay(self.portal.pk, "document_reply_task", [comm.pk]) else: ManualPortal.receive_msg(self, comm, reason="Unexpected email format")
def on_match(match): """Download the files""" if comm.foia.current_tracking_id() != match.group("tracking_id"): ManualPortal.receive_msg(self, comm, reason="Tracking ID does not match") portal_task.delay( self.portal.pk, "document_reply_task", [comm.pk, match.group("documents"), match.group("text")], )
def document_reply_task(self, comm_pk, documents, text): """Download the documents in a task""" comm = FOIACommunication.objects.get(pk=comm_pk) try: session = requests.Session() self._login(comm, session) reply = self._get( session, furl(self.portal.url).add( path=['requests', comm.foia.current_tracking_id()]).url, 'Getting request page to view list of documents', ) documents = [ d.strip('- \r') for d in documents.split('\n') if d.strip() ] soup = BeautifulSoup(reply.content, 'lxml') for document in documents: href = self._find_tag_attr( soup, { 'name': 'a', 'class': 'document-link', 'string': document, }, 'href', 'Attempting to find the document: {}'.format(document), ) url = furl(self.portal.url).add(path=href) reply = self._get( session, url, 'Downloading document: {}'.format(document), ) comm.attach_file( content=reply.content, name=document, source=self.portal.name, ) self._accept_comm(comm, text) except PortalError as exc: ManualPortal.receive_msg( self, comm, reason=exc.args[0], )
def on_match(match): """Download the files""" if comm.foia.current_tracking_id() != match.group('tracking_id'): ManualPortal.receive_msg( self, comm, reason='Tracking ID does not match', ) portal_task.delay( self.portal.pk, 'document_reply_task', [ comm.pk, match.group('documents'), match.group('text'), ], )
def document_reply_task(self, comm_pk): """Download the documents asynchornously""" comm = FOIACommunication.objects.get(pk=comm_pk) p_document_link = re.compile(r"\* \[(?P<name>[^\]]+)\]\((?P<url>.*)\)") for name, url in p_document_link.findall(comm.communication): reply = requests.get(url) if reply.status_code != 200: ManualPortal.receive_msg( self, comm, reason="Error downloading file: {}".format(name)) return comm.attach_file(content=reply.content, name=name, source=self.portal.name) self._accept_comm( comm, "There are eFOIA files available for you to download.")
def on_match(match): """Set the estimated completion date""" if comm.foia.current_tracking_id() != match.group("tracking_id"): ManualPortal.receive_msg(self, comm, reason="Tracking ID does not match") try: comm.foia.date_estimate = datetime.strptime( match.group("date"), "%B %d, %Y").date() except ValueError: ManualPortal.receive_msg(self, comm, reason="Bad date: {}".format( match.group("date"))) else: comm.foia.save() self._accept_comm(comm, match.group("message"))
def on_match(match): """Set the estimated completion date""" if comm.foia.current_tracking_id() != match.group('tracking_id'): ManualPortal.receive_msg( self, comm, reason='Tracking ID does not match', ) try: comm.foia.date_estimate = datetime.strptime( match.group('date'), '%B %d, %Y', ).date() except ValueError: ManualPortal.receive_msg( self, comm, reason='Bad date: {}'.format(match.group('date')), ) else: comm.foia.save() self._accept_comm(comm, match.group('message'))
def document_reply_task(self, comm_pk, documents, text): """Download the documents in a task""" comm = FOIACommunication.objects.get(pk=comm_pk) try: session = requests.Session() self._login(comm, session) reply = self._get( session, furl(self.portal.url).add( path=["requests", comm.foia.current_tracking_id()]).url, "Getting request page to view list of documents", ) documents = [ d.strip("-* \r") for d in documents.split("\n") if d.strip() ] soup = BeautifulSoup(reply.content, "lxml") for document in documents: href = self._find_tag_attr( soup, { "name": "a", "class": "document-link", "string": document }, "href", "Attempting to find the document: {}".format(document), ) url = furl(self.portal.url).add(path=href) reply = self._get(session, url, "Downloading document: {}".format(document)) comm.attach_file(content=reply.content, name=document, source=self.portal.name) self._accept_comm(comm, text) except PortalError as exc: ManualPortal.receive_msg(self, comm, reason=exc.args[0])