def _create_representative (self, row):
        from representative.models import Representative
        """Create a complete Representative record.

        @param row: data row
        @type row: [ str ]
        @return: a representative
        @rtype: representative.Representative
        """
        name = glt.firstname_first(row[1])
        self.stdout.write('%s | Representative: %s ... ' % (row[0], name))

        dob = self._get_isodate(row[9].decode('utf-8'))
        representative = Representative(
            date_of_birth=dob,
            description=row[8].decode('utf-8').strip(),
            unit=self.unit)
        representative.save()

        pname = PersonName(person=representative, name_ka=name, 
                           name_en=glt.to_latin(name), main=True)
        pname.save()

        people = Representative.objects.filter(slug=representative.slug)
        if len(people) > 1:
            if self.force:
                people.exclude(pk=representative.pk).delete()
                self.stdout.write('replace existing!\n')
                self._add_representative_data(row, representative)
                return representative
            else:
                self.stdout.write('keep already existing!\n')
                slug = representative.slug
                representative.delete()
                return Representative.objects.get(slug=slug)
        else:
            self.stdout.write('add new!\n')
            self._add_representative_data(row, representative)
            return representative
Beispiel #2
0
    def get_context_data(self, **kwargs):
        context = super(Detail, self).get_context_data(**kwargs)
        obj = context['obj']

        context['form'] = self._get_form(obj)
        context['questions'] = self._get_questions(obj)
        totalCount = VotingRecordResult.get_counts(representative=obj)

        context['attended'] = totalCount['total'] - totalCount['absent']
        context['absent'] = totalCount['absent']
        if totalCount['total'] == 0:
            context['percentage_attended'] = 0
            context['percentage_absent'] = 0
            context['percentage_attended_string'] = "N/A"
        else:
            context['percentage_attended'] = context['attended'] / float(totalCount['total']) * 100
            context['percentage_absent'] = 100 - context['percentage_attended']
            context['percentage_attended_string'] = "{0:.2f}".format(context['percentage_attended'])
            
        
        # Reformatting Contact phone and address information
        context['contactaddress'] = ""
        if obj.contact_address_phone:
            contactitems = obj.contact_address_phone.split("; ")
            context['contactaddress'] = [items.strip().replace("<p>","").replace("</p>","") for items in contactitems]

        #votecountall = VotingRecordResult.get_counts(representative=obj)
        #context['votecounts'] = votecountall
        context['votecounts'] = VotingRecordResult.get_counts(representative=obj,session=3)
        context['lawvotecounts'] = VotingRecordResult.get_counts(representative=obj,lawcount=True)

        context['url_feed'] = reverse('representative_feed_detail', args=[obj.pk])
        context['url_votingrecords'] = reverse(
            'representative_votingrecords', args=[obj.pk, obj.slug])
        try:
            context['decl'] = obj.incomedeclaration.all()[0]
        except IndexError:
            context['decl'] = None


        try:
            latestdeclarationid = obj.income['declarationid']
            familymembers = obj.family_income.filter(ad_id__exact=latestdeclarationid).exclude(fam_income__exact=0)
            # This loop is temporary. We need to fix the xquery file that write the SQL file for representative_familyincome.
            for member in familymembers:
                member.fam_name_en = glt.to_latin(member.fam_name)
                member.fam_name_ka = member.fam_name
                member.fam_role_en = glt.to_latin(member.fam_role)
                member.fam_role_ka = member.fam_role
            context['faminc'] = familymembers
  
        except IndexError:
            context['faminc'] = None

        try:
            currentincomeyear = obj.income['latestsubmissionyear']
            context['assetlinks'] = obj.urls.filter(label__contains="Asset").exclude(label__contains=currentincomeyear)
        except IndexError:
            context['assetlinks'] = None

        try:
            names_to_exclude = ["Facebook","Twitter","LinkedIn","Wikipedia","Asset Link"]
            context['parliamentlink'] = obj.urls.all().exclude(label__in=names_to_exclude)
        except IndexError:
            context['parliamentlink'] = None

        try:
            names_to_exclude = ["მთავარი","Homepage","Parliament.ge"]
            context['sociallinks'] = obj.urls.all().exclude(label__in=names_to_exclude).exclude(label__contains="Asset")
        except IndexError:
            context['sociallinks'] = None

        set_language_changer(self.request, obj.get_absolute_url)
        return context