Beispiel #1
0
def import_orgs(xml_file):
    outsys("\nRunning {}() ".format(who_am_i()))

    def text_from_xpath(tree, xpath):
        """ utility to get the text of an element using xpath, stripped
            returns '' unless the xpath returns exactly one element
        """
        element = tree.xpath(xpath)
        if len(element) != 1:
            return ''
        return element[0].text.strip() if element[0].text else ''

    def data_from_xpaths(xpaths, etree):
        """ use the xpaths dict to replace the values with the actual data in the etree
        that is retrieved when using text_from_xpath() with the xpath
        """
        return {key: text_from_xpath(etree, xpath) for key, xpath in xpaths.items()}

    def org_data_from_xml(org_etree):
        # keys are Organisation field names, values are xpath expressions for getting those values from the org_etree
        xpaths = dict(
            name='name',
            description='description',
            url='url',
            new_organisation_type='iati_organisation_type',
        )
        # get the raw data from the org_etree
        org_data = data_from_xpaths(xpaths, org_etree)
        # transform data
        org_data['long_name'] = org_data['name']
        org_data['name'] = org_data['name'][:25]
        org_data['organisation_type'] = Organisation.org_type_from_iati_type(int(org_data['new_organisation_type']))
        return org_data

    def create_new_organisation(org_etree, internal_id):
        try:
            org_dict = org_data_from_xml(org_etree)
            referenced_org = Organisation.objects.create(**org_dict)
            log(
                u"Created new organisation: {label}, Akvo ID: {pk}",
                dict(
                    log_type=LOG_ORGANISATIONS,
                    internal_id=internal_id,
                    label=referenced_org.name,
                    pk=referenced_org.pk,
                    event=ACTION_CREATE_ORG
                )
            )
            return referenced_org
        except Exception, e:
            log(
                u"Error trying to create organisation with Cordaid ID {internal_id} ",
                dict(
                    log_type=LOG_ORGANISATIONS,
                    internal_id=internal_id,
                    event=ERROR_EXCEPTION,
                    extra=e.message
                )
            )
Beispiel #2
0
def create_cats_and_benches(business_units):
    outsys("\nRunning {}() ".format(who_am_i()))
    for internal_id, data in business_units.items():
        for name in data['benchmarknames']:
            outsys('.')
            new_bench, created = Benchmarkname.objects.get_or_create(name=name)
            if created:
                log(u"Created bench: {id}, {name}", dict(id=new_bench.id, name=name))
            else:
                log(u"Found existing bench: {id}, {name}", dict(id=new_bench.id, name=name))
            data['category'].benchmarknames.add(new_bench)
Beispiel #3
0
def create_cordaid_business_units(business_units):
    outsys("\nRunning {}() ".format(who_am_i()))
    business_units_info = [
        dict(pk=CORDAID_ORG_ID,  internal_id="27239"),
        dict(pk=959,  internal_id="K6020", cat_name="Children & Education", fa="Education"),
        dict(pk=962,  internal_id="K6090", cat_name="Domestic", fa="Economic development"),
        dict(pk=961,  internal_id="K6030", cat_name="Disaster Recovery", fa="Economic development"),
        dict(pk=950,  internal_id="K6070", cat_name="Entrepreneurship", fa="Economic development"),
        dict(pk=1099, internal_id="K6110", cat_name="Food Security", fa="Healthcare"),
        dict(pk=953,  internal_id="K6100", cat_name="Investments", fa="Economic development"),
        dict(pk=949,  internal_id="K6010", cat_name="Healthcare", fa="Healthcare"),
        dict(pk=1241, internal_id="K6060", cat_name="Security & Justice", fa="Economic development"),
        dict(pk=946,  internal_id="K6080", cat_name="Urban Matters", fa="Economic development"),
        dict(pk=955,  internal_id="K6040", cat_name="Women's leadership", fa="Economic development"),
        dict(pk=960,  internal_id="K6050", cat_name="Extractives", fa="Economic development"),
    ]
    cordaid = Organisation.objects.get(pk=CORDAID_ORG_ID)
    for data in business_units_info:
        outsys('.')
        pk, identifier = data['pk'], data['internal_id']
        cat_name, fa_name = data.get('cat_name'), data.get('fa')
        try:
            organisation = Organisation.objects.get(pk=pk)
        except:
            log(
                u"No business unit with id {pk}, internal ID {identifier}",
                dict(pk=pk, identifier=identifier),
            )
            continue
        internal_org, created = InternalOrganisationID.objects.get_or_create(
            recording_org=cordaid,
            referenced_org=organisation,
            identifier= identifier
        )
        if cat_name:
            new_cat, created = Category.objects.get_or_create(name=cat_name)
            if created:
                log(u"Created cat: {id}, {cat_name}",dict(id=new_cat.id, cat_name=cat_name))
                new_cat.focus_area.add(FocusArea.objects.get(name=fa_name))
            else:
                log(u"Found existing cat: {id}, {cat_name}", dict(id=new_cat.id, cat_name=cat_name))
            business_units.setdefault(identifier, {'category': None, 'benchmarknames': []})['category'] = new_cat

    cordaid.iati_org_id = CORDAID_IATI_ID
    cordaid.save()
    try:
        dgis = Organisation.objects.get(pk=DGIS_ORG_ID)
        dgis.iati_org_id = DGIS_IATI_ID
        dgis.save()
    except:
        log(u"Can't find DGIS using ID {dgis_id}", dict(dgis_id=DGIS_ORG_ID),)
    return business_units
Beispiel #4
0
def import_cordaid_benchmarks(csv_file):
    outsys("\nRunning {}() ".format(who_am_i()))
    # the columns to use in the CSV
    COL_BENCHMARKNAME, COL_BUSINESS_UNID_ID = 1, 2
    with open(csv_file, 'r') as f:
        outsys('.')
        indicators_reader = csv.reader(f, delimiter=',', quotechar='"')
        business_units = {}
        for line in indicators_reader:
            business_units.setdefault(
                line[COL_BUSINESS_UNID_ID], dict(benchmarknames=[]))['benchmarknames'].append(line[COL_BENCHMARKNAME]
            )
        return business_units
Beispiel #5
0
def import_cordaid_benchmarks(csv_file):
    outsys("\nRunning {}() ".format(who_am_i()))
    # the columns to use in the CSV
    COL_BENCHMARKNAME, COL_BUSINESS_UNID_ID = 1, 2
    with open(csv_file, 'r') as f:
        outsys('.')
        indicators_reader = csv.reader(f, delimiter=',', quotechar='"')
        business_units = {}
        for line in indicators_reader:
            business_units.setdefault(
                line[COL_BUSINESS_UNID_ID], dict(benchmarknames=[]))['benchmarknames'].append(line[COL_BENCHMARKNAME]
                                                                                              )
        return business_units
Beispiel #6
0
def get_post_process_data():
    """ Create a dictionary with photo IDs as keys:
    {
        <photo-id>: {
            'internal_project_id': <internal_project_id>,
            'image_caption': <image-caption>,
            'image_credit': <image-credit>,
            'cordaid_funding': <cordaid-funding>,
            'others_funding': <others-funding>,
        }
    },
    """
    outsys("\nRunning {}() ".format(who_am_i()))
    with open(CORDAID_IATI_ACTIVITIES_XML, 'r') as f:
        root = etree.fromstring(f.read())
        AKVO_NS = '{{{akvo_ns}}}'.format(akvo_ns=root.nsmap['akvo'])
        photos = {}
        budgets = []
        publishing_statuses = {}
        for activity in root:
            outsys(".")
            photos[activity.get(AKVO_NS + 'photo-id')] = dict(
                internal_project_id=activity.get(AKVO_NS +
                                                 'internal-project-id'),
                image_caption=activity.get(AKVO_NS + 'image-caption',
                                           '').strip(),
                image_credit=activity.get(AKVO_NS + 'photo-credit',
                                          '').strip(),
            )
            cordaid_budget = activity.findall('budget[@' + AKVO_NS +
                                              'budget-from="Cordaid"]')
            others_budget = activity.findall('budget[@' + AKVO_NS +
                                             'budget-from="Others"]')
            budgets.append(
                dict(
                    internal_project_id=activity.get(AKVO_NS +
                                                     'internal-project-id'),
                    cordaid_funding=Decimal(cordaid_budget[0].find('value').
                                            text if cordaid_budget else 0),
                    others_funding=Decimal(others_budget[0].find('value').
                                           text if others_budget else 0),
                ))
            publishing_statuses[activity.get(
                AKVO_NS +
                'internal-project-id')] = activity.get(AKVO_NS +
                                                       'publish') == 'true'
        return photos, budgets, publishing_statuses
Beispiel #7
0
def get_post_process_data():
    """ Create a dictionary with photo IDs as keys:
    {
        <photo-id>: {
            'internal_project_id': <internal_project_id>,
            'image_caption': <image-caption>,
            'image_credit': <image-credit>,
            'cordaid_funding': <cordaid-funding>,
            'others_funding': <others-funding>,
        }
    },
    """
    outsys("\nRunning {}() ".format(who_am_i()))
    with open(CORDAID_IATI_ACTIVITIES_XML, 'r') as f:
        root = etree.fromstring(f.read())
        AKVO_NS = '{{{akvo_ns}}}'.format(akvo_ns=root.nsmap['akvo'])
        photos = {}
        budgets = []
        publishing_statuses = {}
        for activity in root:
            outsys(".")
            photos[
                activity.get(AKVO_NS + 'photo-id')
            ] = dict(
                internal_project_id=activity.get(AKVO_NS + 'internal-project-id'),
                image_caption=activity.get(AKVO_NS + 'image-caption', '').strip(),
                image_credit=activity.get(AKVO_NS + 'photo-credit', '').strip(),
            )
            cordaid_budget = activity.findall('budget[@' + AKVO_NS + 'budget-from="Cordaid"]')
            others_budget = activity.findall('budget[@' + AKVO_NS + 'budget-from="Others"]')
            budgets.append(
                dict(
                    internal_project_id=activity.get(AKVO_NS + 'internal-project-id'),
                    cordaid_funding=Decimal(cordaid_budget[0].find('value').text if cordaid_budget else 0),
                    others_funding=Decimal(others_budget[0].find('value').text if others_budget else 0),
                )
            )
            publishing_statuses[
                activity.get(AKVO_NS + 'internal-project-id')
            ] = activity.get(AKVO_NS + 'publish') == 'true'
        return photos, budgets, publishing_statuses
Beispiel #8
0
def create_cats_and_benches(business_units):
    outsys("\nRunning {}() ".format(who_am_i()))
    for internal_id, data in business_units.items():
        if data.get('category'):
            for name in data['benchmarknames']:
                outsys('.')
                new_bench, created = Benchmarkname.objects.get_or_create(
                    name=name)
                if created:
                    log(
                        u"Created bench: {pk}, {label}",
                        dict(label=name,
                             pk=new_bench.id,
                             event=ACTION_BENCH_CREATE))
                else:
                    log(
                        u"Found existing bench: {pk}, {label}",
                        dict(label=name,
                             pk=new_bench.id,
                             event=ACTION_BENCH_FOUND))
                try:
                    data['category'].benchmarknames.add(new_bench)
                except:
                    # we shouldn't end up here since we're testing for the existance of the category above
                    pass
        else:
            outsys('*')
            log(
                u"No category set of business unit with internal ID {internal_id}",
                dict(internal_id=internal_id, event=ERROR_CATEGORY_MISSING))
Beispiel #9
0
def set_publishing_status(publishing_statuses):
    outsys("\nRunning {}() ".format(who_am_i()))
    cordaid = Organisation.objects.get(pk=CORDAID_ORG_ID)
    for internal_id, publish in publishing_statuses.items():
        try:
            status = PublishingStatus.objects.get(
                project__partnerships__internal_id=internal_id,
                project__partnerships__organisation=cordaid,
            )
            status.status = PublishingStatus.STATUS_PUBLISHED if publish else PublishingStatus.STATUS_UNPUBLISHED
            status.save()
            log(
                u"Set publishing status for project ID: {pk}: {extra}",
                dict(internal_id=internal_id,
                     pk=status.project.pk,
                     event=ACTION_PUBLISHING_SET,
                     extra=status.status))
            outsys(".")
        except Exception, e:
            log(
                u"Error setting publishing status for project {internal_id}\nException class: {extra}",
                dict(internal_id=internal_id,
                     event=e.__class__,
                     extra=e.message),
            )
            outsys("*")
Beispiel #10
0
def fix_funding(budgets):
    """
    Add Cordaid as a funding partner to all its projects and "fill the project up"
    """
    outsys("\nRunning {}() ".format(who_am_i()))

    def assign_funding_partner(project, organisation, amount):
        funding_partnership, created = Partnership.objects.get_or_create(
            organisation=organisation,
            project=project,
            partner_type=Partnership.FUNDING_PARTNER,
            defaults={'funding_amount': amount}
        )
        if created:
            log(
                u"Added {org_name} as funding partner to project {{pk}}, funding amount: {{extra}}".format(org_name=organisation.name),
                dict(internal_id=internal_id, pk=project.pk, event=ACTION_FUNDING_SET, extra=amount)
            )
        else:
            funding_partnership.funding_amount = amount
            funding_partnership.save()
            log(
                u"Found {org_name} as funding partner to project {{pk}}, setting funding amount: {{extra}}".format(org_name=organisation.name),
                dict(internal_id=internal_id, pk=project.pk, event=ACTION_FUNDING_FOUND, extra=amount)
            )

    cordaid = Organisation.objects.get(pk=CORDAID_ORG_ID)
    others = Organisation.objects.get(pk=OTHERS_ORG_ID)
    for budget in budgets:
        internal_id = budget['internal_project_id']
        try:
            project = None
            project = Project.objects.get(
                partnerships__internal_id=internal_id, partnerships__organisation=cordaid
            )
            cordaid_funding = budget.get('cordaid_funding', 0)
            others_funding = budget.get('others_funding', 0)
            if cordaid_funding:
                assign_funding_partner(project, cordaid, cordaid_funding)
            if others_funding:
                assign_funding_partner(project, others, others_funding)
            total_budget = cordaid_funding + others_funding
            old_budgets = BudgetItem.objects.filter(project=project)
            old_budgets.delete()
            BudgetItem.objects.create(
                project=project,
                label = BudgetItemLabel.objects.get(pk=13), #total budget label
                amount = total_budget
            )
            log(
                u"Total budget for project {pk}: {extra}",
                dict(internal_id=internal_id, pk=project.pk, event=ACTION_BUDGET_SET, extra=total_budget)
            )
            outsys(".")
        except Exception, e:
            log(u"Error setting up funding partners for project {pk}\nException class: {extra}",
                dict(internal_id=internal_id, pk=getattr(project, 'pk', None), event=e.__class__, extra=e.message),
            )
            outsys("*")
Beispiel #11
0
def find_cordaid_business_units(business_units):
    outsys("\nRunning {}() ".format(who_am_i()))
    known_business_units = {
        "27239": dict(pk=CORDAID_ORG_ID),
        "K6020": dict(pk=959, cat_name="Children & Education", fa="Education"),
        "K6090": dict(pk=962, cat_name="Domestic", fa="Economic development"),
        "K6030": dict(pk=961, cat_name="Disaster Recovery", fa="Economic development"),
        "K6070": dict(pk=950, cat_name="Entrepreneurship", fa="Economic development"),
        "K6110": dict(pk=1099, cat_name="Food Security", fa="Healthcare"),
        "K6100": dict(pk=953, cat_name="Investments", fa="Economic development"),
        "K6010": dict(pk=949, cat_name="Healthcare", fa="Healthcare"),
        "K6060": dict(pk=1241, cat_name="Security & Justice", fa="Economic development"),
        "K6080": dict(pk=946, cat_name="Urban Matters", fa="Economic development"),
        "K6040": dict(pk=955, cat_name="Women's leadership", fa="Economic development"),
        "K6050": dict(pk=960, cat_name="Extractives", fa="Economic development"),
    }
    cordaid = Organisation.objects.get(pk=CORDAID_ORG_ID)

    for internal_id in business_units.keys():
        cbu = known_business_units.get(internal_id, {'pk': -1})
        pk, cat_name, fa_name = cbu['pk'], cbu.get('cat_name'), cbu.get('fa'),
        try:
            organisation = Organisation.objects.get(pk=pk)
            outsys('.')
        except:
            outsys('*')
            log(
                u"No business unit with internal ID {internal_id}",
                dict(
                    internal_id=internal_id,
                    event=ERROR_BUSINESS_UNIT_MISSING
                )
            )
            continue
        internal_org, created = InternalOrganisationID.objects.get_or_create(
            recording_org=cordaid,
            referenced_org=organisation,
            identifier= internal_id
        )
        if cat_name:
            new_cat, created = Category.objects.get_or_create(name=cat_name)
            if created:
                log(u"Created cat: {id}, {cat_name}",dict(id=new_cat.id, cat_name=cat_name))
                new_cat.focus_area.add(FocusArea.objects.get(name=fa_name))
            else:
                log(u"Found existing cat: {id}, {cat_name}", dict(id=new_cat.id, cat_name=cat_name))
            business_units.setdefault(internal_id, {'category': None, 'benchmarknames': []})['category'] = new_cat

    cordaid.iati_org_id = CORDAID_IATI_ID
    cordaid.save()
    try:
        dgis = Organisation.objects.get(pk=DGIS_ORG_ID)
        dgis.iati_org_id = DGIS_IATI_ID
        dgis.save()
    except:
        log(u"Can't find DGIS using ID {dgis_id}", dict(dgis_id=DGIS_ORG_ID),)
    return business_units
Beispiel #12
0
def find_cordaid_business_units(business_units):
    outsys("\nRunning {}() ".format(who_am_i()))
    known_business_units = {
        "27239": dict(pk=CORDAID_ORG_ID),
        "K6020": dict(pk=959, cat_name="Children & Education", fa="Education"),
        "K6090": dict(pk=962, cat_name="Domestic", fa="Economic development"),
        "K6030": dict(pk=961, cat_name="Disaster Recovery", fa="Economic development"),
        "K6070": dict(pk=950, cat_name="Entrepreneurship", fa="Economic development"),
        "K6110": dict(pk=1099, cat_name="Food Security", fa="Healthcare"),
        "K6100": dict(pk=953, cat_name="Investments", fa="Economic development"),
        "K6010": dict(pk=949, cat_name="Healthcare", fa="Healthcare"),
        "K6060": dict(pk=1241, cat_name="Security & Justice", fa="Economic development"),
        "K6080": dict(pk=946, cat_name="Urban Matters", fa="Economic development"),
        "K6040": dict(pk=955, cat_name="Women's leadership", fa="Economic development"),
        "K6050": dict(pk=960, cat_name="Extractives", fa="Economic development"),
    }
    cordaid = Organisation.objects.get(pk=CORDAID_ORG_ID)

    for internal_id in business_units.keys():
        cbu = known_business_units.get(internal_id, {'pk': -1})
        pk, cat_name, fa_name = cbu['pk'], cbu.get('cat_name'), cbu.get('fa'),
        try:
            organisation = Organisation.objects.get(pk=pk)
            outsys('.')
        except:
            outsys('*')
            log(
                u"No business unit with internal ID {internal_id}",
                dict(
                    internal_id=internal_id,
                    event=ERROR_BUSINESS_UNIT_MISSING
                )
            )
            continue
        internal_org, created = InternalOrganisationID.objects.get_or_create(
            recording_org=cordaid,
            referenced_org=organisation,
            identifier=internal_id
        )
        if cat_name:
            new_cat, created = Category.objects.get_or_create(name=cat_name)
            if created:
                log(u"Created cat: {id}, {cat_name}", dict(id=new_cat.id, cat_name=cat_name))
                new_cat.focus_area.add(FocusArea.objects.get(name=fa_name))
            else:
                log(u"Found existing cat: {id}, {cat_name}", dict(id=new_cat.id, cat_name=cat_name))
            business_units.setdefault(internal_id, {'category': None, 'benchmarknames': []})['category'] = new_cat

    cordaid.iati_org_id = CORDAID_IATI_ID
    cordaid.save()
    try:
        dgis = Organisation.objects.get(pk=DGIS_ORG_ID)
        dgis.iati_org_id = DGIS_IATI_ID
        dgis.save()
    except:
        log(u"Can't find DGIS using ID {dgis_id}", dict(dgis_id=DGIS_ORG_ID),)
    return business_units
Beispiel #13
0
def import_images(image_dir, photos):
    outsys("\nRunning {}() ".format(who_am_i()))
    for image_name in os.listdir(image_dir):
        photo_id, ext = splitext(image_name)
        if ext.lower() in ['.png', '.jpg', '.jpeg', '.gif']:
            try:
                internal_id = photos.get(
                    photo_id,
                    {'internal_project_id': None})['internal_project_id']
                project = Project.objects.get(
                    partnerships__internal_id=internal_id)
                filename = model_and_instance_based_filename(
                    'Project', project.pk, 'current_image', image_name)
                with open(os.path.join(image_dir, image_name), 'rb') as f:
                    image_data = f.read()
                    image_temp = NamedTemporaryFile(delete=True)
                    image_temp.write(image_data)
                    image_temp.flush()
                    project.current_image.save(filename,
                                               File(image_temp),
                                               save=True)
                f.close()
                project.current_image_caption = photos.get(
                    photo_id, {'image_caption': ''})['image_caption']
                project.current_image_credit = photos.get(
                    photo_id, {'image_credit': ''})['image_credit']
                project.save()
                log(
                    u"Uploaded image to project {pk}",
                    dict(internal_id=internal_id,
                         pk=project.pk,
                         event=ACTION_SET_IMAGE))
                outsys(".")
            except Exception, e:
                log(
                    u"Upload failed. internal_id: {internal_id} Exception class: {extra}",
                    dict(internal_id=internal_id,
                         event=ERROR_IMAGE_UPLOAD,
                         extra=e.__class__),
                )
                outsys("*")
Beispiel #14
0
def set_publishing_status(publishing_statuses):
    outsys("\nRunning {}() ".format(who_am_i()))
    cordaid = Organisation.objects.get(pk=CORDAID_ORG_ID)
    for internal_id, publish in publishing_statuses.items():
        try:
            status = PublishingStatus.objects.get(
                project__partnerships__internal_id=internal_id,
                project__partnerships__organisation=cordaid,
            )
            status.status = PublishingStatus.STATUS_PUBLISHED if publish else PublishingStatus.STATUS_UNPUBLISHED
            status.save()
            log(
                u"Set publishing status for project ID: {pk}: {extra}",
                dict(internal_id=internal_id, pk=status.project.pk, event=ACTION_PUBLISHING_SET, extra=status.status)
            )
            outsys(".")
        except Exception, e:
            log(u"Error setting publishing status for project {internal_id}\nException class: {extra}",
                dict(internal_id=internal_id, event=e.__class__, extra=e.message),
                )
            outsys("*")
Beispiel #15
0
def import_images(image_dir, photos):
    outsys("\nRunning {}() ".format(who_am_i()))
    for image_name in os.listdir(image_dir):
        photo_id, ext = splitext(image_name)
        if ext.lower() in ['.png', '.jpg', '.jpeg', '.gif']:
            try:
                internal_id = photos.get(
                    photo_id, {'internal_project_id': None}
                )['internal_project_id']
                project = Project.objects.get(
                    partnerships__internal_id=internal_id
                )
                filename = model_and_instance_based_filename(
                    'Project', project.pk, 'current_image', image_name
                )
                with open(os.path.join(image_dir, image_name), 'rb') as f:
                    image_data = f.read()
                    image_temp = NamedTemporaryFile(delete=True)
                    image_temp.write(image_data)
                    image_temp.flush()
                    project.current_image.save(filename, File(image_temp), save=True)
                f.close()
                project.current_image_caption = photos.get(
                    photo_id, {'image_caption': ''}
                )['image_caption']
                project.current_image_credit = photos.get(
                    photo_id, {'image_credit': ''}
                )['image_credit']
                project.save()
                log(
                    u"Uploaded image to project {pk}",
                    dict(internal_id=internal_id, pk=project.pk, event=ACTION_SET_IMAGE))
                outsys(".")
            except Exception, e:
                log(
                    u"Upload failed. internal_id: {internal_id} Exception class: {extra}",
                    dict(internal_id=internal_id, event=ERROR_IMAGE_UPLOAD, extra=e.__class__),
                )
                outsys("*")
Beispiel #16
0
def create_cats_and_benches(business_units):
    outsys("\nRunning {}() ".format(who_am_i()))
    for internal_id, data in business_units.items():
        if data.get('category'):
            for name in data['benchmarknames']:
                outsys('.')
                new_bench, created = Benchmarkname.objects.get_or_create(name=name)
                if created:
                    log(u"Created bench: {pk}, {label}",
                        dict(
                            label=name,
                            pk=new_bench.id,
                            event=ACTION_BENCH_CREATE
                        )
                        )
                else:
                    log(u"Found existing bench: {pk}, {label}",
                        dict(
                            label=name,
                            pk=new_bench.id,
                            event=ACTION_BENCH_FOUND
                        )
                        )
                try:
                    data['category'].benchmarknames.add(new_bench)
                except:
                    # we shouldn't end up here since we're testing for the existance of the category above
                    pass
        else:
            outsys('*')
            log(
                u"No category set of business unit with internal ID {internal_id}",
                dict(
                    internal_id=internal_id,
                    event=ERROR_CATEGORY_MISSING
                )
            )
Beispiel #17
0
def fix_funding(budgets):
    """
    Add Cordaid as a funding partner to all its projects and "fill the project up"
    """
    outsys("\nRunning {}() ".format(who_am_i()))

    def assign_funding_partner(project, organisation, amount):
        funding_partnership, created = Partnership.objects.get_or_create(
            organisation=organisation,
            project=project,
            iati_organisation_role=Partnership.IATI_FUNDING_PARTNER,
            defaults={'funding_amount': amount}
        )
        if created:
            log(
                "Added {org_name} as funding partner to project {{pk}}, funding amount: {{extra}}".format(org_name=organisation.name),
                dict(internal_id=internal_id, pk=project.pk, event=ACTION_FUNDING_SET, extra=amount)
            )
        else:
            funding_partnership.funding_amount = amount
            funding_partnership.save()
            log(
                "Found {org_name} as funding partner to project {{pk}}, setting funding amount: {{extra}}".format(org_name=organisation.name),
                dict(internal_id=internal_id, pk=project.pk, event=ACTION_FUNDING_FOUND, extra=amount)
            )

    cordaid = Organisation.objects.get(pk=CORDAID_ORG_ID)
    others = Organisation.objects.get(pk=OTHERS_ORG_ID)
    for budget in budgets:
        internal_id = budget['internal_project_id']
        try:
            project = None
            project = Project.objects.get(
                partnerships__internal_id=internal_id, partnerships__organisation=cordaid
            )
            project.set_reporting_org(cordaid)
            project.save()
            cordaid_funding = budget.get('cordaid_funding', 0)
            others_funding = budget.get('others_funding', 0)
            if cordaid_funding:
                assign_funding_partner(project, cordaid, cordaid_funding)
            if others_funding:
                assign_funding_partner(project, others, others_funding)
            total_budget = cordaid_funding + others_funding
            old_budgets = BudgetItem.objects.filter(project=project)
            old_budgets.delete()
            BudgetItem.objects.create(
                project=project,
                label=BudgetItemLabel.objects.get(pk=BudgetItemLabel.TOTAL_BUDGET_LABEL_ID),
                amount=total_budget
            )
            log(
                "Total budget for project {pk}: {extra}",
                dict(internal_id=internal_id, pk=project.pk, event=ACTION_BUDGET_SET, extra=total_budget)
            )
            outsys(".")
        except Exception as e:
            log("Error setting up funding partners for project {pk}\nException class: {extra}",
                dict(internal_id=internal_id, pk=getattr(project, 'pk', None), event=e.__class__, extra=str(e)),
                )
            outsys("*")
    outsys('\n')
Beispiel #18
0
            BudgetItem.objects.create(
                project=project,
                label=BudgetItemLabel.objects.get(pk=BudgetItemLabel.TOTAL_BUDGET_LABEL_ID),
                amount=total_budget
            )
            log(
                u"Total budget for project {pk}: {extra}",
                dict(internal_id=internal_id, pk=project.pk, event=ACTION_BUDGET_SET, extra=total_budget)
            )
            outsys(".")
        except Exception, e:
            log(u"Error setting up funding partners for project {pk}\nException class: {extra}",
                dict(internal_id=internal_id, pk=getattr(project, 'pk', None), event=e.__class__, extra=e.message),
                )
            outsys("*")
    outsys('\n')


def set_publishing_status(publishing_statuses):
    outsys("\nRunning {}() ".format(who_am_i()))
    cordaid = Organisation.objects.get(pk=CORDAID_ORG_ID)
    for internal_id, publish in publishing_statuses.items():
        try:
            status = PublishingStatus.objects.get(
                project__partnerships__internal_id=internal_id,
                project__partnerships__organisation=cordaid,
            )
            status.status = PublishingStatus.STATUS_PUBLISHED if publish else PublishingStatus.STATUS_UNPUBLISHED
            status.save()
            log(
                u"Set publishing status for project ID: {pk}: {extra}",
Beispiel #19
0
                u"Total budget for project {pk}: {extra}",
                dict(internal_id=internal_id,
                     pk=project.pk,
                     event=ACTION_BUDGET_SET,
                     extra=total_budget))
            outsys(".")
        except Exception, e:
            log(
                u"Error setting up funding partners for project {pk}\nException class: {extra}",
                dict(internal_id=internal_id,
                     pk=getattr(project, 'pk', None),
                     event=e.__class__,
                     extra=e.message),
            )
            outsys("*")
    outsys('\n')


def set_publishing_status(publishing_statuses):
    outsys("\nRunning {}() ".format(who_am_i()))
    cordaid = Organisation.objects.get(pk=CORDAID_ORG_ID)
    for internal_id, publish in publishing_statuses.items():
        try:
            status = PublishingStatus.objects.get(
                project__partnerships__internal_id=internal_id,
                project__partnerships__organisation=cordaid,
            )
            status.status = PublishingStatus.STATUS_PUBLISHED if publish else PublishingStatus.STATUS_UNPUBLISHED
            status.save()
            log(
                u"Set publishing status for project ID: {pk}: {extra}",