def get_dhis2_connection(domain_name): from corehq.motech.dhis2.models import Dhis2Connection result = Dhis2Connection.get_db().view( 'by_domain_doc_type_date/view', key=[domain_name, 'Dhis2Connection', None], include_docs=True, reduce=False, ).first() return Dhis2Connection.wrap(result['doc']) if result else None
def save(self, domain_name): try: dhis2_conn = get_dhis2_connection(domain_name) if dhis2_conn is None: dhis2_conn = Dhis2Connection(domain=domain_name) dhis2_conn.server_url = self.cleaned_data['server_url'] dhis2_conn.username = self.cleaned_data['username'] if self.cleaned_data['password']: # Don't save it if it hasn't been changed. Use simple symmetric encryption. We don't need it to be # strong, considering we'd have to store the algorithm and the key together anyway; it just # shouldn't be plaintext. dhis2_conn.password = b64encode(bz2.compress(self.cleaned_data['password'])) dhis2_conn.save() return True except Exception as err: logging.error('Unable to save DHIS2 connection: %s' % err) return False