|
| 1 | +# organization/models.py |
| 2 | +# Brought to you by We Vote. Be good. |
| 3 | +# -*- coding: UTF-8 -*- |
| 4 | + |
| 5 | +from django.db import models |
| 6 | +from exception.models import handle_record_found_more_than_one_exception |
| 7 | +import wevote_functions.admin |
| 8 | +from wevote_settings.models import fetch_next_id_we_vote_last_org_integer, fetch_site_unique_id_prefix |
| 9 | + |
| 10 | + |
| 11 | +logger = wevote_functions.admin.get_logger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class Organization(models.Model): |
| 15 | + # We are relying on built-in Python id field |
| 16 | + |
| 17 | + # The id_we_vote identifier is unique across all We Vote sites, and allows us to share our org info with other |
| 18 | + # organizations |
| 19 | + # It starts with "wv" then we add on a database specific identifier like "3v" (WeVoteSetting.site_unique_id_prefix) |
| 20 | + # then the string "org", and then a sequential integer like "123". |
| 21 | + # We keep the last value in WeVoteSetting.id_we_vote_last_org_integer |
| 22 | + id_we_vote = models.CharField( |
| 23 | + verbose_name="we vote permanent id", max_length=255, default=None, null=True, blank=True, unique=True) |
| 24 | + name = models.CharField( |
| 25 | + verbose_name="organization name", max_length=255, default=None, null=True, blank=True) |
| 26 | + url = models.URLField(verbose_name='url of the endorsing organization', blank=True, null=True) |
| 27 | + twitter_handle = models.CharField(max_length=15, null=True, unique=False, verbose_name='twitter handle') |
| 28 | + |
| 29 | + NONPROFIT_501C3 = '3' |
| 30 | + NONPROFIT_501C4 = '4' |
| 31 | + POLITICAL_ACTION_COMMITTEE = 'P' |
| 32 | + CORPORATION = 'C' |
| 33 | + NEWS_CORPORATION = 'N' |
| 34 | + UNKNOWN = 'U' |
| 35 | + ORGANIZATION_TYPE_CHOICES = ( |
| 36 | + (NONPROFIT_501C3, 'Nonprofit 501c3'), |
| 37 | + (NONPROFIT_501C4, 'Nonprofit 501c4'), |
| 38 | + (POLITICAL_ACTION_COMMITTEE, 'Political Action Committee'), |
| 39 | + (CORPORATION, 'Corporation'), |
| 40 | + (NEWS_CORPORATION, 'News Corporation'), |
| 41 | + (UNKNOWN, 'Unknown'), |
| 42 | + ) |
| 43 | + |
| 44 | + organization_type = models.CharField( |
| 45 | + verbose_name="type of org", max_length=1, choices=ORGANIZATION_TYPE_CHOICES, default=UNKNOWN) |
| 46 | + |
| 47 | + # Link to a logo for this organization |
| 48 | + # logo |
| 49 | + |
| 50 | + def __unicode__(self): |
| 51 | + return self.name |
| 52 | + |
| 53 | + class Meta: |
| 54 | + ordering = ('name',) |
| 55 | + |
| 56 | + # We override the save function so we can auto-generate id_we_vote |
| 57 | + def save(self, *args, **kwargs): |
| 58 | + # Even if this organization came from another source we still need a unique id_we_vote |
| 59 | + if self.id_we_vote: |
| 60 | + self.id_we_vote = self.id_we_vote.strip() |
| 61 | + if self.id_we_vote == "" or self.id_we_vote is None: # If there isn't a value... |
| 62 | + # ...generate a new id |
| 63 | + site_unique_id_prefix = fetch_site_unique_id_prefix() |
| 64 | + next_local_integer = fetch_next_id_we_vote_last_org_integer() |
| 65 | + # "wv" = We Vote |
| 66 | + # site_unique_id_prefix = a generated (or assigned) unique id for one server running We Vote |
| 67 | + # "org" = tells us this is a unique id for an org |
| 68 | + # next_integer = a unique, sequential integer for this server - not necessarily tied to database id |
| 69 | + self.id_we_vote = "wv{site_unique_id_prefix}org{next_integer}".format( |
| 70 | + site_unique_id_prefix=site_unique_id_prefix, |
| 71 | + next_integer=next_local_integer, |
| 72 | + ) |
| 73 | + # TODO we need to deal with the situation where id_we_vote is NOT unique on save |
| 74 | + super(Organization, self).save(*args, **kwargs) |
| 75 | + |
| 76 | + def is_nonprofit_501c3(self): |
| 77 | + return self.organization_type in self.NONPROFIT_501C3 |
| 78 | + |
| 79 | + def is_nonprofit_501c4(self): |
| 80 | + return self.organization_type in self.NONPROFIT_501C4 |
| 81 | + |
| 82 | + def is_political_action_committee(self): |
| 83 | + return self.organization_type in self.POLITICAL_ACTION_COMMITTEE |
| 84 | + |
| 85 | + def is_corporation(self): |
| 86 | + return self.organization_type in self.CORPORATION |
| 87 | + |
| 88 | + def is_news_corporation(self): |
| 89 | + return self.organization_type in self.NEWS_CORPORATION |
| 90 | + |
| 91 | + def is_organization_type_specified(self): |
| 92 | + return self.organization_type in ( |
| 93 | + self.NONPROFIT_501C3, self.NONPROFIT_501C4, self.POLITICAL_ACTION_COMMITTEE, |
| 94 | + self.CORPORATION, self.NEWS_CORPORATION) |
| 95 | + |
| 96 | + |
| 97 | +class OrganizationManager(models.Model): |
| 98 | + """ |
| 99 | + A class for working with the Organization model |
| 100 | + """ |
| 101 | + def retrieve_organization(self, organization_id, id_we_vote=None): |
| 102 | + error_result = False |
| 103 | + exception_does_not_exist = False |
| 104 | + exception_multiple_object_returned = False |
| 105 | + organization_on_stage = Organization() |
| 106 | + organization_on_stage_id = 0 |
| 107 | + try: |
| 108 | + if organization_id > 0: |
| 109 | + organization_on_stage = Organization.objects.get(id=organization_id) |
| 110 | + organization_on_stage_id = organization_on_stage.id |
| 111 | + elif len(id_we_vote) > 0: |
| 112 | + organization_on_stage = Organization.objects.get(id_we_vote=id_we_vote) |
| 113 | + organization_on_stage_id = organization_on_stage.id |
| 114 | + except Organization.MultipleObjectsReturned as e: |
| 115 | + handle_record_found_more_than_one_exception(e, logger) |
| 116 | + error_result = True |
| 117 | + exception_multiple_object_returned = True |
| 118 | + logger.warn("Organization.MultipleObjectsReturned") |
| 119 | + except Organization.DoesNotExist: |
| 120 | + error_result = True |
| 121 | + exception_does_not_exist = True |
| 122 | + logger.warn("Organization.DoesNotExist") |
| 123 | + |
| 124 | + organization_on_stage_found = True if organization_on_stage_id > 0 else False |
| 125 | + results = { |
| 126 | + 'success': True if organization_on_stage_found else False, |
| 127 | + 'organization_found': organization_on_stage_found, |
| 128 | + 'organization_id': organization_on_stage_id, |
| 129 | + 'organization': organization_on_stage, |
| 130 | + 'error_result': error_result, |
| 131 | + 'DoesNotExist': exception_does_not_exist, |
| 132 | + 'MultipleObjectsReturned': exception_multiple_object_returned, |
| 133 | + } |
| 134 | + return results |
| 135 | + |
| 136 | + def fetch_organization_id(self, id_we_vote): |
| 137 | + organization_id = 0 |
| 138 | + organization_manager = OrganizationManager() |
| 139 | + if len(id_we_vote) > 0: |
| 140 | + results = organization_manager.retrieve_organization(organization_id, id_we_vote) # TODO DALE |
| 141 | + if results['success']: |
| 142 | + return results['organization_id'] |
| 143 | + return 0 |
0 commit comments