from candidates.views import PartyDetailView

class UKPartyDetailView(PartyDetailView):

    def get_context_data(self, **kwargs):
        context = super(UKPartyDetailView, self).get_context_data(**kwargs)

        party_ec_id = context['party'].identifiers.get(scheme='electoral-commission')
        context['ec_url'] = None
        if party_ec_id:
            ec_tmpl = 'http://search.electoralcommission.org.uk/English/Registrations/{0}'
            context['ec_url'] = ec_tmpl.format(party_ec_id)
        context['register'] = context['party'].extra.register

        return context

from candidates.views import PartyDetailView
from popolo.models import Identifier

class UKPartyDetailView(PartyDetailView):

    def get_context_data(self, **kwargs):
        context = super(UKPartyDetailView, self).get_context_data(**kwargs)

        context['ec_url'] = ''
        context['register'] = ''
        try:
            party_ec_id = context['party'].identifiers.get(scheme='electoral-commission')
            if party_ec_id:
                ec_tmpl = 'http://search.electoralcommission.org.uk/English/Registrations/{0}'
                context['ec_url'] = ec_tmpl.format(party_ec_id)
            context['register'] = context['party'].extra.register
        except Identifier.DoesNotExist:
            pass

        return context

Fix the 'Independent' party pages for UK elections
Fix the 'Independent' party pages for UK elections

There's no Electoral Commission identifier for the 'Independent'
pseudo-party, so the party page for independents was failing.

  from candidates.views import PartyDetailView
+ from popolo.models import Identifier
  
  class UKPartyDetailView(PartyDetailView):
  
      def get_context_data(self, **kwargs):
          context = super(UKPartyDetailView, self).get_context_data(**kwargs)
  
+         context['ec_url'] = ''
+         context['register'] = ''
+         try:
-         party_ec_id = context['party'].identifiers.get(scheme='electoral-commission')
+             party_ec_id = context['party'].identifiers.get(scheme='electoral-commission')
-         context['ec_url'] = None
-         if party_ec_id:
+             if party_ec_id:
-             ec_tmpl = 'http://search.electoralcommission.org.uk/English/Registrations/{0}'
+                 ec_tmpl = 'http://search.electoralcommission.org.uk/English/Registrations/{0}'
-             context['ec_url'] = ec_tmpl.format(party_ec_id)
+                 context['ec_url'] = ec_tmpl.format(party_ec_id)
-         context['register'] = context['party'].extra.register
+             context['register'] = context['party'].extra.register
+         except Identifier.DoesNotExist:
+             pass
  
          return context
  
Fix the 'Independent' party pages for UK elections
## Code Before:
from candidates.views import PartyDetailView

class UKPartyDetailView(PartyDetailView):

    def get_context_data(self, **kwargs):
        context = super(UKPartyDetailView, self).get_context_data(**kwargs)

        party_ec_id = context['party'].identifiers.get(scheme='electoral-commission')
        context['ec_url'] = None
        if party_ec_id:
            ec_tmpl = 'http://search.electoralcommission.org.uk/English/Registrations/{0}'
            context['ec_url'] = ec_tmpl.format(party_ec_id)
        context['register'] = context['party'].extra.register

        return context

## Instruction:
Fix the 'Independent' party pages for UK elections
## Code After:
from candidates.views import PartyDetailView
from popolo.models import Identifier

class UKPartyDetailView(PartyDetailView):

    def get_context_data(self, **kwargs):
        context = super(UKPartyDetailView, self).get_context_data(**kwargs)

        context['ec_url'] = ''
        context['register'] = ''
        try:
            party_ec_id = context['party'].identifiers.get(scheme='electoral-commission')
            if party_ec_id:
                ec_tmpl = 'http://search.electoralcommission.org.uk/English/Registrations/{0}'
                context['ec_url'] = ec_tmpl.format(party_ec_id)
            context['register'] = context['party'].extra.register
        except Identifier.DoesNotExist:
            pass

        return context
