def test_set_sector_transaction_without_percentages(self):
        """
        percentages should be split equally among the existing 2 sectors.
        As a result percentages should then be split equally.
        """
        self.setUpTransactionModels()
        post_save.set_sector_transaction(self.activity)
        self.assertEqual(TransactionSector.objects.count(), 8)

        ts1 = TransactionSector.objects.filter(sector=self.s1,
                                               transaction=self.t1)[0]
        # 10000 / 2
        self.assertEqual(Decimal(ts1.percentage), 50)

        ts2 = TransactionSector.objects.filter(sector=self.s2,
                                               transaction=self.t1)[0]
        # 20000 / 3
        self.assertEqual(round(Decimal(ts2.percentage)), 50)

        TransactionSector.objects.filter(sector=self.s3,
                                         transaction=self.t1)[0]
        # 10000 / 2
        self.assertEqual(Decimal(ts1.percentage), 50)

        TransactionSector.objects.filter(sector=self.s4,
                                         transaction=self.t1)[0]
        # 20000 / 3
        self.assertEqual(round(Decimal(ts2.percentage)), 50)
Exemplo n.º 2
0
def change_activity_id(old_id, new_id):
    #
    activity = Activity.objects.get(pk=old_id)
    activity.id = new_id
    activity.save()
    #
    new_activity = activity
    old_activity = Activity.objects.get(pk=old_id)
    #
    print new_activity
    #
    change_related_model_parent_activity(new_activity,
                                         old_activity.reporting_organisations)
    change_one_to_one_parent_activity(new_activity, old_activity.title)
    change_related_model_parent_activity(new_activity,
                                         old_activity.description_set)
    change_related_model_parent_activity(
        new_activity, old_activity.participating_organisations)
    change_related_model_parent_activity(new_activity,
                                         old_activity.activitydate_set)
    change_related_model_parent_activity(new_activity,
                                         old_activity.contactinfo_set)
    change_m2m_parent_activity(new_activity,
                               old_activity.activityrecipientcountry_set)
    change_m2m_parent_activity(new_activity,
                               old_activity.activityrecipientregion_set)
    change_related_model_parent_activity(new_activity,
                                         old_activity.location_set)
    change_m2m_parent_activity(new_activity, old_activity.activitysector_set)
    change_related_model_parent_activity(new_activity,
                                         old_activity.humanitarianscope_set)
    change_m2m_parent_activity(new_activity,
                               old_activity.activitypolicymarker_set)
    change_related_model_parent_activity(new_activity,
                                         old_activity.planneddisbursement_set)
    change_related_model_parent_activity(new_activity, old_activity.budget_set)
    change_related_model_parent_activity(new_activity,
                                         old_activity.transaction_set)
    change_related_model_parent_activity(new_activity,
                                         old_activity.documentlink_set)
    change_related_activity_parent_activity(new_activity,
                                            old_activity.relatedactivity_set)
    change_related_model_parent_activity(new_activity, old_activity.result_set)
    #
    post_save.set_related_activities(new_activity)
    post_save.set_participating_organisation_activity_id(
        new_activity.participating_organisations)
    post_save.set_transaction_provider_receiver_activity(new_activity)
    post_save.set_derived_activity_dates(new_activity)
    post_save.set_activity_aggregations(new_activity)
    post_save.update_activity_search_index(new_activity)
    post_save.set_country_region_transaction(new_activity)
    post_save.set_sector_transaction(new_activity)
    post_save.set_sector_budget(new_activity)
    old_activity.delete()
Exemplo n.º 3
0
    def save_related(self, request, form, formsets, change):
        super(TransactionAdmin, self).save_related(request, form, formsets,
                                                   change)

        # remove old
        TransactionSector.objects.filter(
            transaction__activity=self.act).delete()
        TransactionRecipientCountry.objects.filter(
            transaction__activity=self.act).delete()
        TransactionRecipientRegion.objects.filter(
            transaction__activity=self.act).delete()

        # add new
        post_save.set_transaction_provider_receiver_activity(self.act)
        post_save.set_activity_aggregations(self.act)
        post_save.set_country_region_transaction(self.act)
        post_save.set_sector_transaction(self.act)
Exemplo n.º 4
0
def convert_xdr_values(apps, schema_editor):
    """
    convert all transaction's xdr values
    takes ~20 mins
    """
    update_contenttypes(apps.get_app_config('iati'),
                        interactive=False)  # make sure all content types exist

    try:  # don't run on first migration
        Budget = apps.get_model('iati', 'Budget')
        Transaction = apps.get_model('iati', 'Transaction')
        Activity = apps.get_model('iati', 'Activity')

        TranasctionSector = apps.get_model('iati', 'TranasctionSector')
        TransactionRecipientCountry = apps.get_model(
            'iati', 'TransactionRecipientCountry')
        TransactionRecipientSector = apps.get_model(
            'iati', 'TransactionRecipientSector')

    except:
        return

    for budget in Budget.objects.all().iterator():
        budget.xdr_value = convert.to_xdr(budget.currency_id,
                                          budget.value_date, budget.value)
        budget.save()

    for transaction in Transaction.objects.all().iterator():
        transaction.xdr_value = convert.to_xdr(transaction.currency_id,
                                               transaction.value_date,
                                               transaction.value)
        transaction.save()

    for activity in Activity.objects.all().iterator():
        TransactionSector.objects.all().filter(
            transaction__activity=activity).delete()
        TransactionRecipientCountry.objects.all().filter(
            transaction__activity=activity).delete()
        TransactionRecipientSector.objects.all().filter(
            transaction__activity=activity).delete()

        post_save.set_sector_transaction(activity)
        post_save.set_country_region_transaction(activity)
Exemplo n.º 5
0
    def save_related(self, request, form, formsets, change):
        super(ActivityAdmin, self).save_related(request, form, formsets,
                                                change)
        post_save.set_derived_activity_dates(self.act)
        post_save.set_activity_aggregations(self.act)
        post_save.update_activity_search_index(self.act)

        # remove old
        BudgetSector.objects.filter(budget__activity=self.act).delete()
        TransactionSector.objects.filter(
            transaction__activity=self.act).delete()
        TransactionRecipientCountry.objects.filter(
            transaction__activity=self.act).delete()
        TransactionRecipientRegion.objects.filter(
            transaction__activity=self.act).delete()

        # add new
        post_save.set_country_region_transaction(self.act)
        post_save.set_sector_transaction(self.act)
        post_save.set_sector_budget(self.act)
Exemplo n.º 6
0
    def test_set_sector_transaction_with_percentages(self):
        """
        percentage of a transaction should be splitted according to the percentages given on the ActivitySector
        """
        self.setUpTransactionModels()
        self.rs1.percentage = 25
        self.rs1.save()
        self.rs2.percentage = 75
        self.rs2.save()
        self.rs3.percentage = 25
        self.rs3.save()
        self.rs4.percentage = 75
        self.rs4.save()

        post_save.set_sector_transaction(self.activity)

        self.assertEqual(TransactionSector.objects.count(), 8)

        self.assertEqual(TransactionSector.objects.filter(
            sector=self.s1,
            vocabulary=self.sector_vocabulary_1,
            transaction=self.t1,
            percentage=25
        ).count(), 1)
        self.assertEqual(TransactionSector.objects.filter(
            sector=self.s2,
            vocabulary=self.sector_vocabulary_1,
            transaction=self.t1,
            percentage=75
        ).count(), 1)
        self.assertEqual(TransactionSector.objects.filter(
            sector=self.s3,
            transaction=self.t1,
            percentage=25
        ).count(), 1)
        self.assertEqual(TransactionSector.objects.filter(
            sector=self.s4,
            transaction=self.t1,
            percentage=75
        ).count(), 1)