Skip to content

Commit

Permalink
project event fixtures; contractor linked to project
Browse files Browse the repository at this point in the history
  • Loading branch information
open-risk committed May 25, 2022
1 parent 5a8fff9 commit 7c33a8e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 18 deletions.
4 changes: 2 additions & 2 deletions portfolio/Contractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class Contractor(models.Model):

# LINKS

project = models.ForeignKey('ProjectActivity', blank=True, null=True, on_delete=models.CASCADE,
help_text="Project Activity that engaged the Contractor (Optional)")
project = models.ForeignKey('Project', blank=True, null=True, on_delete=models.CASCADE,
help_text="Project that engaged the Contractor (Optional)")

project_company = models.ForeignKey('ProjectCompany', blank=True, null=True, on_delete=models.CASCADE,
help_text="Project Company that sourced the Contractor (Optional)")
Expand Down
29 changes: 21 additions & 8 deletions portfolio/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,28 @@
from django.forms.widgets import NumberInput
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.html import format_html
from treebeard.admin import TreeAdmin
from treebeard.forms import movenodeform_factory

from portfolio.Asset import ProjectAsset, Building
from portfolio.Borrower import Borrower
from portfolio.Contractor import Contractor
from portfolio.EmissionsSource import EmissionsSource, BuildingEmissionsSource
from portfolio.EmissionsSource import GPCEmissionsSource, GPPEmissionsSource
from portfolio.Contractor import Contractor
from portfolio.Borrower import Borrower
from portfolio.Loan import Loan
from portfolio.Mortgage import Mortgage
from portfolio.Operator import Operator
from portfolio.Portfolios import ProjectPortfolio, PortfolioTable
from portfolio.PortfolioManager import PortfolioManager
from portfolio.Portfolios import PortfolioSnapshot, LimitStructure
from portfolio.Portfolios import ProjectPortfolio, PortfolioTable
from portfolio.PrimaryEffect import PrimaryEffect
from portfolio.Project import Project
from portfolio.ProjectEvent import ProjectEvent
from portfolio.ProjectActivity import ProjectActivity
from portfolio.ProjectCategory import ProjectCategory
from portfolio.ProjectCompany import ProjectCompany
from portfolio.ProjectEvent import ProjectEvent
from portfolio.Revenue import Revenue
from portfolio.PrimaryEffect import PrimaryEffect
from portfolio.SecondaryEffect import SecondaryEffect
from portfolio.Sponsor import Sponsor
from portfolio.Stakeholders import Stakeholders
Expand Down Expand Up @@ -199,7 +200,7 @@ class ProjectAdmin(admin.ModelAdmin):
"""Project admin"""
view_on_site = False
save_as = True
list_display = ('project_title', 'cpv_code', 'project_budget', 'project_category')
list_display = ('pk', 'project_title', 'cpv_code', 'project_budget', 'project_category')
date_hierarchy = ('creation_date')


Expand Down Expand Up @@ -231,9 +232,15 @@ class SecondaryEffectAdmin(admin.ModelAdmin):
@admin.register(ProjectActivity)
class ProjectActivityAdmin(admin.ModelAdmin):
"""Project Activity admin"""

def link_to_project(self, obj):
link = reverse("admin:portfolio_project_change", args=[obj.project.pk])
return format_html('<a href="{}">{}</a>', link, obj.project.pk)

link_to_project.short_description = 'Project'
view_on_site = False
save_as = True
list_display = ('project_activity_title', 'region', 'main_site')
list_display = ('pk', 'project_activity_title', 'link_to_project', 'region', 'main_site')
date_hierarchy = ('creation_date')


Expand Down Expand Up @@ -281,9 +288,15 @@ class SponsorAdmin(admin.ModelAdmin):

@admin.register(Contractor)
class ContractorAdmin(admin.ModelAdmin):
def link_to_project(self, obj):
link = reverse("admin:portfolio_project_change", args=[obj.project.pk])
return format_html('<a href="{}">{}</a>', link, obj.project.pk)

link_to_project.short_description = 'Project'

view_on_site = False
save_as = True
list_display = ('name_of_contractor', 'is_sme', 'address', 'town', 'region', 'country')
list_display = ('pk', 'name_of_contractor', 'is_sme', 'link_to_project', 'address', 'town', 'region', 'country')
date_hierarchy = ('creation_date')


Expand Down
7 changes: 5 additions & 2 deletions portfolio/management/commands/load_co.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from django.core.management.base import BaseCommand

from portfolio.Contractor import Contractor
from portfolio.Project import Project


class Command(BaseCommand):
Expand All @@ -40,8 +41,10 @@ class Command(BaseCommand):
indata = []
serial = 10000
for index, entry in data.iterrows():
pr = Project.objects.get(pk=entry['PROJECT'])
co = Contractor(
contractor_identifier=str(serial),
contractor_identifier=entry['PK'],
project=pr,
is_sme=entry['SME'],
contractor_legal_entity_identifier=entry['NATIONALID'],
name_of_contractor=entry['OFFICIALNAME'],
Expand All @@ -61,4 +64,4 @@ class Command(BaseCommand):
Contractor.objects.bulk_create(indata)

def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS('Successfully inserted data into db'))
self.stdout.write(self.style.SUCCESS('Successfully inserted contractor data into db'))
6 changes: 3 additions & 3 deletions portfolio/management/commands/load_pa.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class Command(BaseCommand):

for index, entry in data.iterrows():

pr = Project.objects.get(project_identifier=entry['PROJECT'])
pr = Project.objects.get(pk=entry['PROJECT'])

# TODO fix null issue with markdown field
pa = ProjectActivity(
project_activity_identifier=str(serial),
project_activity_identifier=entry['PK'],
project=pr,
project_activity_title=entry['TITLE'],
project_activity_description=entry['SHORT_DESCR'],
project=pr,
region=entry['NUTS'],
baseline_procedure_justification="",
main_site=entry['MAIN_SITE'])
Expand Down
63 changes: 63 additions & 0 deletions portfolio/management/commands/load_pe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright (c) 2021 - 2022 Open Risk (https://www.openriskmanagement.com)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import pandas as pd
from django.core.management.base import BaseCommand

from portfolio.ProjectEvent import ProjectEvent
from portfolio.ProjectCategory import ProjectCategory
from portfolio.Project import Project


class Command(BaseCommand):
help = 'Imports project activity data'

# Delete existing objects
ProjectEvent.objects.all().delete()

# Import data from file
data = pd.read_csv("pe.csv", header='infer', delimiter=',')

"""
TITLE,NUTS,MAIN_SITE,SHORT_DESCR
"""
indata = []
serial = 10000

for index, entry in data.iterrows():

pr = Project.objects.get(pk=entry['PROJECT'])

pe = ProjectEvent(
project_event_identifier=entry['PK'],
project=pr,
project_event_type=entry['TYPE'],
project_event_date=entry['PUB_DATE'],
project_event_description=entry['SHORT_DESCR'])

serial += 1

indata.append(pe)

ProjectEvent.objects.bulk_create(indata)

def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS('Successfully inserted project event data into db'))
8 changes: 5 additions & 3 deletions portfolio/management/commands/load_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from portfolio.Project import Project
from portfolio.ProjectCategory import ProjectCategory
from portfolio.Portfolios import ProjectPortfolio
from portfolio.Portfolios import ProjectPortfolio


class Command(BaseCommand):
Expand All @@ -41,7 +41,7 @@ class Command(BaseCommand):
"""
indata = []
serial = 10000
serial = 0

pk1 = ProjectCategory.objects.get(name="SUPPLIES")
pk2 = ProjectCategory.objects.get(name="WORKS")
Expand All @@ -58,7 +58,8 @@ class Command(BaseCommand):
fk = pk3

if 'MANAGER' in entry.keys():
po = ProjectPortfolio.objects.get(manager=entry['MANAGER'])
# po = ProjectPortfolio.objects.get(manager=entry['MANAGER'])
po = ProjectPortfolio.objects.get(pk=entry['MANAGER'])
else:
po = ProjectPortfolio.objects.first()

Expand All @@ -68,6 +69,7 @@ class Command(BaseCommand):
project_identifier = str(serial)

pr = Project(
id=entry['PK'],
project_identifier=project_identifier,
project_reference=entry['REFERENCE_NUMBER'],
project_title=entry['TITLE'],
Expand Down

0 comments on commit 7c33a8e

Please sign in to comment.