# -*- coding: utf-8 -*-

# Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.


from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
from django.utils.translation import ugettext_lazy as _

from ..fields import ValidXMLCharField

from akvo.codelists import models as codelist_models
from akvo.codelists.store.codelists_v202 import SECTOR_VOCABULARY
from akvo.utils import codelist_choices, codelist_value


class Sector(models.Model):
    project = models.ForeignKey('Project', verbose_name=_(u'project'), related_name='sectors')
    sector_code = ValidXMLCharField(
        _(u'sector code'), blank=True, max_length=25,
        help_text=_(u'It is possible to specify a variety of sector codes, based on the selected '
                    u'vocabulary. The sector codes for the DAC-5 and DAC-3 vocabularies can be '
                    u'found here: <a href="http://iatistandard.org/202/codelists/Sector/" '
                    u'target="_blank">DAC-5 sector codes</a> and '
                    u'<a href="http://iatistandard.org/202/codelists/SectorCategory/" '
                    u'target="_blank">DAC-3 sector codes</a>.')
    )
    text = ValidXMLCharField(
        _(u'sector description'), blank=True, max_length=100,
        help_text=_(u'Optionally enter a description.')
    )
    vocabulary = ValidXMLCharField(
        _(u'sector vocabulary'), blank=True, max_length=5,
        choices=codelist_choices(SECTOR_VOCABULARY),
        help_text=_(u'This is the code for the vocabulary used to describe the sector. Sectors '
                    u'should be mapped to DAC sectors to enable international comparison.')
    )
    vocabulary_uri = ValidXMLCharField(
        _(u'sector vocabulary URI'), blank=True, max_length=1000,
        help_text=_(u'If the vocabulary is 99 (reporting organisation), the URI where this '
                    u'internal vocabulary is defined.')
    )
    percentage = models.DecimalField(
        _(u'sector percentage'), blank=True, null=True, max_digits=4, decimal_places=1,
        validators=[MaxValueValidator(100), MinValueValidator(0)],
        help_text=_(u'Percentages should add up to 100% of the activity being reported if they are '
                    u'shown for each sector. Fill in 100% if there\'s one sector. Use a period to '
                    u'denote decimals.')
    )

    def __unicode__(self):
        if self.sector_code:
            try:
                sector_unicode = self.iati_sector().name.capitalize()
            except Exception as e:
                sector_unicode = u'%s' % _(u'Sector code not found')
        else:
            sector_unicode = u'%s' % _(u'No sector code specified')

        if self.percentage:
            sector_unicode += u' (%s%%)' % str(self.percentage)

        return sector_unicode


    def iati_sector_codes(self):
        if self.sector_code and (self.vocabulary == '1' or self.vocabulary == 'DAC'):
            return self.sector_code, codelist_value(codelist_models.Sector, self, 'sector_code')
        elif self.sector_code and (self.vocabulary == '2' or self.vocabulary == 'DAC-3'):
            return self.sector_code, codelist_value(codelist_models.SectorCategory,
                                                    self,
                                                    'sector_code')
        else:
            return self.sector_code, self.sector_code

    def iati_sector(self):
        if self.sector_code and (self.vocabulary == '1' or self.vocabulary == 'DAC'):
            return codelist_value(codelist_models.Sector, self, 'sector_code')
        elif self.sector_code and (self.vocabulary == '2' or self.vocabulary == 'DAC-3'):
            return codelist_value(codelist_models.SectorCategory, self, 'sector_code')
        else:
            return self.sector_code

    def iati_vocabulary(self):
        return codelist_value(codelist_models.SectorVocabulary, self, 'vocabulary')

    class Meta:
        app_label = 'rsr'
        verbose_name = _(u'sector')
        verbose_name_plural = _(u'sectors')
        ordering = ['sector_code', ]

@@ -2634,16 +2634,61 @@
 r_code:%0A
+            # Check if the code is specified%0A
         
@@ -2719,25 +2719,30 @@
  sector_
-unicode =
+text = u'%25s' %25
  self.ia
@@ -2794,22 +2794,22 @@
 ept 
-Exception as e
+AttributeError
 :%0A  
@@ -2833,260 +2833,632 @@
 tor_
-unicode = u'%25s' %25 _(u'Sector code not found')%0A        else:%0A            
+text = self.text%0A%0A            try:%0A                vocabulary = self.iati_vocabulary().name%0A            except AttributeError:%0A                vocabulary = ''%0A%0A            return u'%7B0%7D%7B1%7D%7B2%7D%7B3%7D'.format(%0A                u'%7B0%7D: '.format(vocabulary) if vocabulary else u'',%0A                u'%7B0%7D'.format(self.
 sector_
-uni
 code
- = u'%25s' %25 _(u'No sector code specified')%0A%0A        if self.percentage:%0A            sector_unicode += u' (%25s%25%25)' %25 str(self.percentage)%0A%0A        return sector_unicode%0A
+),%0A                u' - %7B0%7D'.format(sector_text) if sector_text else u'',%0A                u' (%7B0%7D%25)'.format(str(self.percentage)) if self.percentage else u'',%0A            )%0A        else:%0A            # In case no code is specified, return this%0A            return u'%7B0%7D'.format(_(u'No sector code specified'))
 %0A%0A  

Update unicode of Sectors