We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Extra info is the same for all candidates
When using extra_info such as ribbon or colors the same values are returned for all candidates in cases such as election page or soul-mate JSON.
The correct value is returned in cases where information for only one candidate is requested, such as the candidate detail page.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `elections/models.py`
Content:
```
1 # coding=utf-8
2 from django.db import models
3 from autoslug import AutoSlugField
4 from taggit.managers import TaggableManager
5 from django.core.urlresolvers import reverse
6 from popolo.models import Person, Area
7 from django.utils.translation import ugettext as _
8 from markdown_deux.templatetags.markdown_deux_tags import markdown_allowed
9 from candidator.models import Category, Topic as CanTopic
10 from picklefield.fields import PickledObjectField
11 from django.conf import settings
12 
13 
14 class ExtraInfoMixin(models.Model):
15     extra_info = PickledObjectField(default={})
16 
17     class Meta:
18         abstract = True
19 
20     def __init__(self, *args, **kwargs):
21         super(ExtraInfoMixin, self).__init__(*args, **kwargs)
22         default_extra_info = self.default_extra_info
23         default_extra_info.update(self.extra_info)
24         self.extra_info = default_extra_info
25 
26 
27 class Candidate(Person, ExtraInfoMixin):
28     election = models.ForeignKey('Election', related_name='candidates', null=True)
29 
30     default_extra_info = settings.DEFAULT_CANDIDATE_EXTRA_INFO
31 
32     @property
33     def twitter(self):
34         links = self.contact_details.filter(contact_type="TWITTER")
35         if links:
36             return links.first()
37 
38 
39 class PersonalData(models.Model):
40     candidate = models.ForeignKey('Candidate', related_name="personal_datas")
41     label = models.CharField(max_length=512)
42     value = models.CharField(max_length=1024)
43 
44 
45 class Topic(CanTopic):
46     class Meta:
47         proxy = True
48 
49     @property
50     def election(self):
51         category = QuestionCategory.objects.get(category_ptr=self.category)
52         return category.election
53 
54 
55 class QuestionCategory(Category):
56     election = models.ForeignKey('Election', related_name='categories', null=True)
57 
58 
59 class Election(ExtraInfoMixin, models.Model):
60     name = models.CharField(max_length=255)
61     slug = AutoSlugField(populate_from='name', unique=True)
62     description = models.TextField(blank=True)
63     tags = TaggableManager(blank=True)
64     searchable = models.BooleanField(default=True)
65     highlighted = models.BooleanField(default=False)
66     extra_info_title = models.CharField(max_length=50, blank=True, null=True)
67     extra_info_content = models.TextField(max_length=3000, blank=True, null=True, help_text=_("Puedes usar Markdown. <br/> ")
68             + markdown_allowed())
69     uses_preguntales = models.BooleanField(default=True, help_text=_(u"Esta elección debe usar preguntales?"))
70     uses_ranking = models.BooleanField(default=True, help_text=_(u"Esta elección debe usar ranking"))
71     uses_face_to_face = models.BooleanField(default=True, help_text=_(u"Esta elección debe usar frente a frente"))
72     uses_soul_mate = models.BooleanField(default=True, help_text=_(u"Esta elección debe usar 1/2 naranja"))
73     uses_questionary = models.BooleanField(default=True, help_text=_(u"Esta elección debe usar cuestionario"))
74 
75     default_extra_info = settings.DEFAULT_ELECTION_EXTRA_INFO
76     area = models.ForeignKey(Area, null=True, related_name="elections")
77 
78     def __unicode__(self):
79         return self.name
80 
81     def get_absolute_url(self):
82         return reverse('election_view', kwargs={'slug': self.slug})
83 
84     def get_extra_info_url(self):
85             return reverse('election_extra_info', kwargs={'slug': self.slug})
86 
87     class Meta:
88             verbose_name = _(u'Mi Elección')
89             verbose_name_plural = _(u'Mis Elecciones')
90
```

Path: `elections/admin.py`
Content:
```
1 
2 from django.contrib import admin
3 from elections.models import Election, Candidate, PersonalData, QuestionCategory
4 from popolo.models import Organization, Membership, ContactDetail, OtherName, Post, Area
5 from django.contrib.contenttypes.admin import GenericTabularInline
6 from django import forms
7 from django.conf import settings
8 from candidator.models import Position, TakenPosition
9 from elections.models import Topic
10 
11 
12 class TakenPositionModelForm(forms.ModelForm):
13 
14     def __init__(self, *args, **kwargs):
15         super(TakenPositionModelForm, self).__init__(*args, **kwargs)
16         if self.instance.id:
17             positions = self.instance.topic.positions.all()
18             self.fields['position'].queryset = positions
19 
20     class Meta:
21         model = TakenPosition
22         fields = ('topic', 'position', 'person')
23 
24 
25 class TakenPositionInlineModelForm(forms.ModelForm):
26     def __init__(self, *args, **kwargs):
27         super(TakenPositionInlineModelForm, self).__init__(*args, **kwargs)
28         if 'instance' in kwargs:
29             positions_qs = kwargs['instance'].topic.positions.all()
30             self.fields['position'].queryset = positions_qs
31 
32     class Meta:
33         model = TakenPosition
34         fields = ('position', 'description')
35 
36     def save(self, force_insert=False, force_update=False, commit=True):
37         m = super(TakenPositionInlineModelForm, self).save(commit=False)
38         if m.position is not None:
39             m.topic = m.position.topic
40         m.save()
41         return m
42 
43 
44 class TakenPositionCandidateInline(admin.TabularInline):
45     model = TakenPosition
46     form = TakenPositionInlineModelForm
47     extra = 0
48 
49     def formfield_for_foreignkey(self, db_field, request, **kwargs):
50         if db_field.name == 'position':
51             pass
52         return super(TakenPositionCandidateInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
53 
54 
55 class TakenPositionAdmin(admin.ModelAdmin):
56     form = TakenPositionModelForm
57 admin.site.register(TakenPosition, TakenPositionAdmin)
58 
59 
60 class TakenPositionInline(admin.TabularInline):
61     model = TakenPosition
62 
63 
64 class PositionAdmin(admin.ModelAdmin):
65     inlines = [TakenPositionInline, ]
66 
67 admin.site.register(Position, PositionAdmin)
68 
69 
70 class PositionInline(admin.TabularInline):
71     model = Position
72 
73 
74 class TopicAdmin(admin.ModelAdmin):
75     inlines = [PositionInline, ]
76     list_display = ('__str__', 'election')
77 
78     def save_model(self, request, obj, form, change):
79         creating = not change
80         obj.save()
81         if creating:
82             for candidate in obj.election.candidates.all():
83                 TakenPosition.objects.get_or_create(topic=obj, person=candidate)
84 
85 admin.site.register(Topic, TopicAdmin)
86 
87 
88 class TopicInline(admin.TabularInline):
89     model = Topic
90 
91 
92 class QuestionCategoryAdmin(admin.ModelAdmin):
93     inlines = [TopicInline, ]
94     list_display = ('__str__', 'election')
95 admin.site.register(QuestionCategory, QuestionCategoryAdmin)
96 
97 
98 class QuestionCategoryInline(admin.TabularInline):
99     model = QuestionCategory
100 
101     list_display = ('__str__', 'election')
102 
103 
104 class CandidateModelForm(forms.ModelForm):
105     model = Candidate
106 
107     def __init__(self, *args, **kwargs):
108         super(CandidateModelForm, self).__init__(*args, **kwargs)
109         self.extra_info_fields = settings.DEFAULT_CANDIDATE_EXTRA_INFO.keys()
110         for key in self.extra_info_fields:
111             self.fields.update({key: forms.CharField(max_length=512,
112                                                      label=key,
113                                                      required=False,
114                                                      widget=forms.TextInput(),
115                                                      initial=self.instance.extra_info[key]
116                                                      )})
117 
118     def save(self, commit=True, *args, **kwargs):
119         instance = super(CandidateModelForm, self).save(commit, *args, **kwargs)
120         for key in self.extra_info_fields:
121             instance.extra_info[key] = self.cleaned_data.get(key, None)
122         if commit:
123             instance.save()
124 
125         return instance
126 
127 
128 class ElectionModelForm(forms.ModelForm):
129     model = Election
130 
131     def __init__(self, *args, **kwargs):
132         super(ElectionModelForm, self).__init__(*args, **kwargs)
133         self.extra_info_fields = settings.DEFAULT_ELECTION_EXTRA_INFO.keys()
134         for key in self.extra_info_fields:
135             self.fields.update({key: forms.CharField(max_length=512,
136                                                      label=key,
137                                                      required=False,
138                                                      widget=forms.TextInput(),
139                                                      initial=self.instance.extra_info[key]
140                                                      )})
141 
142     def save(self, commit=True, *args, **kwargs):
143         instance = super(ElectionModelForm, self).save(commit, *args, **kwargs)
144         for key in self.extra_info_fields:
145             instance.extra_info[key] = self.cleaned_data.get(key, None)
146         if commit:
147             instance.save()
148         return instance
149 
150 
151 class ElectionAdmin(admin.ModelAdmin):
152     form = ElectionModelForm
153     search_fields = ['name', 'tags']
154     inlines = [QuestionCategoryInline, ]
155 
156     def get_fieldsets(self, request, obj=None):
157         fieldsets = super(ElectionAdmin, self).get_fieldsets(request, obj)
158         if hasattr(request, "_gfs_marker"):
159             for key in settings.DEFAULT_ELECTION_EXTRA_INFO.keys():
160                 fieldsets[0][1]['fields'] += (key,)
161         setattr(request, "_gfs_marker", 1)
162         return fieldsets
163 
164 admin.site.register(Election, ElectionAdmin)
165 
166 
167 class OrgnizationAdmin(admin.ModelAdmin):
168     pass
169 admin.site.register(Organization, OrgnizationAdmin)
170 
171 
172 class ContactDetailInline(GenericTabularInline):
173     model = ContactDetail
174     fields = ('label', 'contact_type', 'value')
175 
176 
177 class MembershipInline(admin.TabularInline):
178     model = Membership
179     fields = ('label', 'role', 'organization', 'on_behalf_of', 'post', 'start_date', 'end_date', 'area')
180 
181 
182 class OtherNameInline(GenericTabularInline):
183     model = OtherName
184 
185 
186 class PersonalDataInline(admin.TabularInline):
187     model = PersonalData
188 
189 
190 class CandidateAdmin(admin.ModelAdmin):
191     form = CandidateModelForm
192     inlines = [
193         ContactDetailInline,
194         MembershipInline,
195         OtherNameInline,
196         PersonalDataInline,
197         TakenPositionCandidateInline,
198     ]
199     search_fields = ['name', 'election__name']
200     ordering = ['name']
201 
202     def get_fieldsets(self, request, obj=None):
203         fieldsets = super(CandidateAdmin, self).get_fieldsets(request, obj)
204         if hasattr(request, "_gfs_marker"):
205             for key in settings.DEFAULT_CANDIDATE_EXTRA_INFO.keys():
206                 fieldsets[0][1]['fields'] += (key,)
207         setattr(request, "_gfs_marker", 1)
208         return fieldsets
209 
210     def save_model(self, request, obj, form, change):
211         creating = not change
212         obj.save()
213         if creating:
214             for cat in obj.election.categories.all():
215                 for topic in cat.topics.all():
216                     TakenPosition.objects.get_or_create(topic=topic, person=obj)
217 
218 admin.site.register(Candidate, CandidateAdmin)
219 
220 
221 class PostAdmin(admin.ModelAdmin):
222     pass
223 admin.site.register(Post, PostAdmin)
224 
225 
226 class AreaAdmin(admin.ModelAdmin):
227     pass
228 admin.site.register(Area, AreaAdmin)
229
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/elections/admin.py b/elections/admin.py
--- a/elections/admin.py
+++ b/elections/admin.py
@@ -45,6 +45,7 @@
     model = TakenPosition
     form = TakenPositionInlineModelForm
     extra = 0
+    can_delete = False
 
     def formfield_for_foreignkey(self, db_field, request, **kwargs):
         if db_field.name == 'position':
diff --git a/elections/models.py b/elections/models.py
--- a/elections/models.py
+++ b/elections/models.py
@@ -9,6 +9,8 @@
 from candidator.models import Category, Topic as CanTopic
 from picklefield.fields import PickledObjectField
 from django.conf import settings
+from django.utils.encoding import python_2_unicode_compatible
+import copy
 
 
 class ExtraInfoMixin(models.Model):
@@ -19,7 +21,7 @@
 
     def __init__(self, *args, **kwargs):
         super(ExtraInfoMixin, self).__init__(*args, **kwargs)
-        default_extra_info = self.default_extra_info
+        default_extra_info = copy.copy(self.default_extra_info)
         default_extra_info.update(self.extra_info)
         self.extra_info = default_extra_info
 
@@ -52,9 +54,13 @@
         return category.election
 
 
+@python_2_unicode_compatible
 class QuestionCategory(Category):
     election = models.ForeignKey('Election', related_name='categories', null=True)
 
+    def __str__(self):
+        return u'<%s> in <%s>' % (self.name, self.election.name)
+
 
 class Election(ExtraInfoMixin, models.Model):
     name = models.CharField(max_length=255)

{"golden_diff": "diff --git a/elections/admin.py b/elections/admin.py\n--- a/elections/admin.py\n+++ b/elections/admin.py\n@@ -45,6 +45,7 @@\n     model = TakenPosition\n     form = TakenPositionInlineModelForm\n     extra = 0\n+    can_delete = False\n \n     def formfield_for_foreignkey(self, db_field, request, **kwargs):\n         if db_field.name == 'position':\ndiff --git a/elections/models.py b/elections/models.py\n--- a/elections/models.py\n+++ b/elections/models.py\n@@ -9,6 +9,8 @@\n from candidator.models import Category, Topic as CanTopic\n from picklefield.fields import PickledObjectField\n from django.conf import settings\n+from django.utils.encoding import python_2_unicode_compatible\n+import copy\n \n \n class ExtraInfoMixin(models.Model):\n@@ -19,7 +21,7 @@\n \n     def __init__(self, *args, **kwargs):\n         super(ExtraInfoMixin, self).__init__(*args, **kwargs)\n-        default_extra_info = self.default_extra_info\n+        default_extra_info = copy.copy(self.default_extra_info)\n         default_extra_info.update(self.extra_info)\n         self.extra_info = default_extra_info\n \n@@ -52,9 +54,13 @@\n         return category.election\n \n \n+@python_2_unicode_compatible\n class QuestionCategory(Category):\n     election = models.ForeignKey('Election', related_name='categories', null=True)\n \n+    def __str__(self):\n+        return u'<%s> in <%s>' % (self.name, self.election.name)\n+\n \n class Election(ExtraInfoMixin, models.Model):\n     name = models.CharField(max_length=255)\n", "issue": "Extra info is the same for all candidates\nWhen using extra_info such as ribbon or colors the same values are returned for all candidates in cases such as election page or soul-mate JSON.\nThe correct value is returned in cases where information for only one candidate is requested, such as the candidate detail page.\n\n", "before_files": [{"content": "# coding=utf-8\nfrom django.db import models\nfrom autoslug import AutoSlugField\nfrom taggit.managers import TaggableManager\nfrom django.core.urlresolvers import reverse\nfrom popolo.models import Person, Area\nfrom django.utils.translation import ugettext as _\nfrom markdown_deux.templatetags.markdown_deux_tags import markdown_allowed\nfrom candidator.models import Category, Topic as CanTopic\nfrom picklefield.fields import PickledObjectField\nfrom django.conf import settings\n\n\nclass ExtraInfoMixin(models.Model):\n    extra_info = PickledObjectField(default={})\n\n    class Meta:\n        abstract = True\n\n    def __init__(self, *args, **kwargs):\n        super(ExtraInfoMixin, self).__init__(*args, **kwargs)\n        default_extra_info = self.default_extra_info\n        default_extra_info.update(self.extra_info)\n        self.extra_info = default_extra_info\n\n\nclass Candidate(Person, ExtraInfoMixin):\n    election = models.ForeignKey('Election', related_name='candidates', null=True)\n\n    default_extra_info = settings.DEFAULT_CANDIDATE_EXTRA_INFO\n\n    @property\n    def twitter(self):\n        links = self.contact_details.filter(contact_type=\"TWITTER\")\n        if links:\n            return links.first()\n\n\nclass PersonalData(models.Model):\n    candidate = models.ForeignKey('Candidate', related_name=\"personal_datas\")\n    label = models.CharField(max_length=512)\n    value = models.CharField(max_length=1024)\n\n\nclass Topic(CanTopic):\n    class Meta:\n        proxy = True\n\n    @property\n    def election(self):\n        category = QuestionCategory.objects.get(category_ptr=self.category)\n        return category.election\n\n\nclass QuestionCategory(Category):\n    election = models.ForeignKey('Election', related_name='categories', null=True)\n\n\nclass Election(ExtraInfoMixin, models.Model):\n    name = models.CharField(max_length=255)\n    slug = AutoSlugField(populate_from='name', unique=True)\n    description = models.TextField(blank=True)\n    tags = TaggableManager(blank=True)\n    searchable = models.BooleanField(default=True)\n    highlighted = models.BooleanField(default=False)\n    extra_info_title = models.CharField(max_length=50, blank=True, null=True)\n    extra_info_content = models.TextField(max_length=3000, blank=True, null=True, help_text=_(\"Puedes usar Markdown. <br/> \")\n            + markdown_allowed())\n    uses_preguntales = models.BooleanField(default=True, help_text=_(u\"Esta elecci\u00f3n debe usar preguntales?\"))\n    uses_ranking = models.BooleanField(default=True, help_text=_(u\"Esta elecci\u00f3n debe usar ranking\"))\n    uses_face_to_face = models.BooleanField(default=True, help_text=_(u\"Esta elecci\u00f3n debe usar frente a frente\"))\n    uses_soul_mate = models.BooleanField(default=True, help_text=_(u\"Esta elecci\u00f3n debe usar 1/2 naranja\"))\n    uses_questionary = models.BooleanField(default=True, help_text=_(u\"Esta elecci\u00f3n debe usar cuestionario\"))\n\n    default_extra_info = settings.DEFAULT_ELECTION_EXTRA_INFO\n    area = models.ForeignKey(Area, null=True, related_name=\"elections\")\n\n    def __unicode__(self):\n        return self.name\n\n    def get_absolute_url(self):\n        return reverse('election_view', kwargs={'slug': self.slug})\n\n    def get_extra_info_url(self):\n            return reverse('election_extra_info', kwargs={'slug': self.slug})\n\n    class Meta:\n            verbose_name = _(u'Mi Elecci\u00f3n')\n            verbose_name_plural = _(u'Mis Elecciones')\n", "path": "elections/models.py"}, {"content": "\nfrom django.contrib import admin\nfrom elections.models import Election, Candidate, PersonalData, QuestionCategory\nfrom popolo.models import Organization, Membership, ContactDetail, OtherName, Post, Area\nfrom django.contrib.contenttypes.admin import GenericTabularInline\nfrom django import forms\nfrom django.conf import settings\nfrom candidator.models import Position, TakenPosition\nfrom elections.models import Topic\n\n\nclass TakenPositionModelForm(forms.ModelForm):\n\n    def __init__(self, *args, **kwargs):\n        super(TakenPositionModelForm, self).__init__(*args, **kwargs)\n        if self.instance.id:\n            positions = self.instance.topic.positions.all()\n            self.fields['position'].queryset = positions\n\n    class Meta:\n        model = TakenPosition\n        fields = ('topic', 'position', 'person')\n\n\nclass TakenPositionInlineModelForm(forms.ModelForm):\n    def __init__(self, *args, **kwargs):\n        super(TakenPositionInlineModelForm, self).__init__(*args, **kwargs)\n        if 'instance' in kwargs:\n            positions_qs = kwargs['instance'].topic.positions.all()\n            self.fields['position'].queryset = positions_qs\n\n    class Meta:\n        model = TakenPosition\n        fields = ('position', 'description')\n\n    def save(self, force_insert=False, force_update=False, commit=True):\n        m = super(TakenPositionInlineModelForm, self).save(commit=False)\n        if m.position is not None:\n            m.topic = m.position.topic\n        m.save()\n        return m\n\n\nclass TakenPositionCandidateInline(admin.TabularInline):\n    model = TakenPosition\n    form = TakenPositionInlineModelForm\n    extra = 0\n\n    def formfield_for_foreignkey(self, db_field, request, **kwargs):\n        if db_field.name == 'position':\n            pass\n        return super(TakenPositionCandidateInline, self).formfield_for_foreignkey(db_field, request, **kwargs)\n\n\nclass TakenPositionAdmin(admin.ModelAdmin):\n    form = TakenPositionModelForm\nadmin.site.register(TakenPosition, TakenPositionAdmin)\n\n\nclass TakenPositionInline(admin.TabularInline):\n    model = TakenPosition\n\n\nclass PositionAdmin(admin.ModelAdmin):\n    inlines = [TakenPositionInline, ]\n\nadmin.site.register(Position, PositionAdmin)\n\n\nclass PositionInline(admin.TabularInline):\n    model = Position\n\n\nclass TopicAdmin(admin.ModelAdmin):\n    inlines = [PositionInline, ]\n    list_display = ('__str__', 'election')\n\n    def save_model(self, request, obj, form, change):\n        creating = not change\n        obj.save()\n        if creating:\n            for candidate in obj.election.candidates.all():\n                TakenPosition.objects.get_or_create(topic=obj, person=candidate)\n\nadmin.site.register(Topic, TopicAdmin)\n\n\nclass TopicInline(admin.TabularInline):\n    model = Topic\n\n\nclass QuestionCategoryAdmin(admin.ModelAdmin):\n    inlines = [TopicInline, ]\n    list_display = ('__str__', 'election')\nadmin.site.register(QuestionCategory, QuestionCategoryAdmin)\n\n\nclass QuestionCategoryInline(admin.TabularInline):\n    model = QuestionCategory\n\n    list_display = ('__str__', 'election')\n\n\nclass CandidateModelForm(forms.ModelForm):\n    model = Candidate\n\n    def __init__(self, *args, **kwargs):\n        super(CandidateModelForm, self).__init__(*args, **kwargs)\n        self.extra_info_fields = settings.DEFAULT_CANDIDATE_EXTRA_INFO.keys()\n        for key in self.extra_info_fields:\n            self.fields.update({key: forms.CharField(max_length=512,\n                                                     label=key,\n                                                     required=False,\n                                                     widget=forms.TextInput(),\n                                                     initial=self.instance.extra_info[key]\n                                                     )})\n\n    def save(self, commit=True, *args, **kwargs):\n        instance = super(CandidateModelForm, self).save(commit, *args, **kwargs)\n        for key in self.extra_info_fields:\n            instance.extra_info[key] = self.cleaned_data.get(key, None)\n        if commit:\n            instance.save()\n\n        return instance\n\n\nclass ElectionModelForm(forms.ModelForm):\n    model = Election\n\n    def __init__(self, *args, **kwargs):\n        super(ElectionModelForm, self).__init__(*args, **kwargs)\n        self.extra_info_fields = settings.DEFAULT_ELECTION_EXTRA_INFO.keys()\n        for key in self.extra_info_fields:\n            self.fields.update({key: forms.CharField(max_length=512,\n                                                     label=key,\n                                                     required=False,\n                                                     widget=forms.TextInput(),\n                                                     initial=self.instance.extra_info[key]\n                                                     )})\n\n    def save(self, commit=True, *args, **kwargs):\n        instance = super(ElectionModelForm, self).save(commit, *args, **kwargs)\n        for key in self.extra_info_fields:\n            instance.extra_info[key] = self.cleaned_data.get(key, None)\n        if commit:\n            instance.save()\n        return instance\n\n\nclass ElectionAdmin(admin.ModelAdmin):\n    form = ElectionModelForm\n    search_fields = ['name', 'tags']\n    inlines = [QuestionCategoryInline, ]\n\n    def get_fieldsets(self, request, obj=None):\n        fieldsets = super(ElectionAdmin, self).get_fieldsets(request, obj)\n        if hasattr(request, \"_gfs_marker\"):\n            for key in settings.DEFAULT_ELECTION_EXTRA_INFO.keys():\n                fieldsets[0][1]['fields'] += (key,)\n        setattr(request, \"_gfs_marker\", 1)\n        return fieldsets\n\nadmin.site.register(Election, ElectionAdmin)\n\n\nclass OrgnizationAdmin(admin.ModelAdmin):\n    pass\nadmin.site.register(Organization, OrgnizationAdmin)\n\n\nclass ContactDetailInline(GenericTabularInline):\n    model = ContactDetail\n    fields = ('label', 'contact_type', 'value')\n\n\nclass MembershipInline(admin.TabularInline):\n    model = Membership\n    fields = ('label', 'role', 'organization', 'on_behalf_of', 'post', 'start_date', 'end_date', 'area')\n\n\nclass OtherNameInline(GenericTabularInline):\n    model = OtherName\n\n\nclass PersonalDataInline(admin.TabularInline):\n    model = PersonalData\n\n\nclass CandidateAdmin(admin.ModelAdmin):\n    form = CandidateModelForm\n    inlines = [\n        ContactDetailInline,\n        MembershipInline,\n        OtherNameInline,\n        PersonalDataInline,\n        TakenPositionCandidateInline,\n    ]\n    search_fields = ['name', 'election__name']\n    ordering = ['name']\n\n    def get_fieldsets(self, request, obj=None):\n        fieldsets = super(CandidateAdmin, self).get_fieldsets(request, obj)\n        if hasattr(request, \"_gfs_marker\"):\n            for key in settings.DEFAULT_CANDIDATE_EXTRA_INFO.keys():\n                fieldsets[0][1]['fields'] += (key,)\n        setattr(request, \"_gfs_marker\", 1)\n        return fieldsets\n\n    def save_model(self, request, obj, form, change):\n        creating = not change\n        obj.save()\n        if creating:\n            for cat in obj.election.categories.all():\n                for topic in cat.topics.all():\n                    TakenPosition.objects.get_or_create(topic=topic, person=obj)\n\nadmin.site.register(Candidate, CandidateAdmin)\n\n\nclass PostAdmin(admin.ModelAdmin):\n    pass\nadmin.site.register(Post, PostAdmin)\n\n\nclass AreaAdmin(admin.ModelAdmin):\n    pass\nadmin.site.register(Area, AreaAdmin)\n", "path": "elections/admin.py"}], "after_files": [{"content": "# coding=utf-8\nfrom django.db import models\nfrom autoslug import AutoSlugField\nfrom taggit.managers import TaggableManager\nfrom django.core.urlresolvers import reverse\nfrom popolo.models import Person, Area\nfrom django.utils.translation import ugettext as _\nfrom markdown_deux.templatetags.markdown_deux_tags import markdown_allowed\nfrom candidator.models import Category, Topic as CanTopic\nfrom picklefield.fields import PickledObjectField\nfrom django.conf import settings\nfrom django.utils.encoding import python_2_unicode_compatible\nimport copy\n\n\nclass ExtraInfoMixin(models.Model):\n    extra_info = PickledObjectField(default={})\n\n    class Meta:\n        abstract = True\n\n    def __init__(self, *args, **kwargs):\n        super(ExtraInfoMixin, self).__init__(*args, **kwargs)\n        default_extra_info = copy.copy(self.default_extra_info)\n        default_extra_info.update(self.extra_info)\n        self.extra_info = default_extra_info\n\n\nclass Candidate(Person, ExtraInfoMixin):\n    election = models.ForeignKey('Election', related_name='candidates', null=True)\n\n    default_extra_info = settings.DEFAULT_CANDIDATE_EXTRA_INFO\n\n    @property\n    def twitter(self):\n        links = self.contact_details.filter(contact_type=\"TWITTER\")\n        if links:\n            return links.first()\n\n\nclass PersonalData(models.Model):\n    candidate = models.ForeignKey('Candidate', related_name=\"personal_datas\")\n    label = models.CharField(max_length=512)\n    value = models.CharField(max_length=1024)\n\n\nclass Topic(CanTopic):\n    class Meta:\n        proxy = True\n\n    @property\n    def election(self):\n        category = QuestionCategory.objects.get(category_ptr=self.category)\n        return category.election\n\n\n@python_2_unicode_compatible\nclass QuestionCategory(Category):\n    election = models.ForeignKey('Election', related_name='categories', null=True)\n\n    def __str__(self):\n        return u'<%s> in <%s>' % (self.name, self.election.name)\n\n\nclass Election(ExtraInfoMixin, models.Model):\n    name = models.CharField(max_length=255)\n    slug = AutoSlugField(populate_from='name', unique=True)\n    description = models.TextField(blank=True)\n    tags = TaggableManager(blank=True)\n    searchable = models.BooleanField(default=True)\n    highlighted = models.BooleanField(default=False)\n    extra_info_title = models.CharField(max_length=50, blank=True, null=True)\n    extra_info_content = models.TextField(max_length=3000, blank=True, null=True, help_text=_(\"Puedes usar Markdown. <br/> \")\n            + markdown_allowed())\n    uses_preguntales = models.BooleanField(default=True, help_text=_(u\"Esta elecci\u00f3n debe usar preguntales?\"))\n    uses_ranking = models.BooleanField(default=True, help_text=_(u\"Esta elecci\u00f3n debe usar ranking\"))\n    uses_face_to_face = models.BooleanField(default=True, help_text=_(u\"Esta elecci\u00f3n debe usar frente a frente\"))\n    uses_soul_mate = models.BooleanField(default=True, help_text=_(u\"Esta elecci\u00f3n debe usar 1/2 naranja\"))\n    uses_questionary = models.BooleanField(default=True, help_text=_(u\"Esta elecci\u00f3n debe usar cuestionario\"))\n\n    default_extra_info = settings.DEFAULT_ELECTION_EXTRA_INFO\n    area = models.ForeignKey(Area, null=True, related_name=\"elections\")\n\n    def __unicode__(self):\n        return self.name\n\n    def get_absolute_url(self):\n        return reverse('election_view', kwargs={'slug': self.slug})\n\n    def get_extra_info_url(self):\n            return reverse('election_extra_info', kwargs={'slug': self.slug})\n\n    class Meta:\n            verbose_name = _(u'Mi Elecci\u00f3n')\n            verbose_name_plural = _(u'Mis Elecciones')\n", "path": "elections/models.py"}, {"content": "\nfrom django.contrib import admin\nfrom elections.models import Election, Candidate, PersonalData, QuestionCategory\nfrom popolo.models import Organization, Membership, ContactDetail, OtherName, Post, Area\nfrom django.contrib.contenttypes.admin import GenericTabularInline\nfrom django import forms\nfrom django.conf import settings\nfrom candidator.models import Position, TakenPosition\nfrom elections.models import Topic\n\n\nclass TakenPositionModelForm(forms.ModelForm):\n\n    def __init__(self, *args, **kwargs):\n        super(TakenPositionModelForm, self).__init__(*args, **kwargs)\n        if self.instance.id:\n            positions = self.instance.topic.positions.all()\n            self.fields['position'].queryset = positions\n\n    class Meta:\n        model = TakenPosition\n        fields = ('topic', 'position', 'person')\n\n\nclass TakenPositionInlineModelForm(forms.ModelForm):\n    def __init__(self, *args, **kwargs):\n        super(TakenPositionInlineModelForm, self).__init__(*args, **kwargs)\n        if 'instance' in kwargs:\n            positions_qs = kwargs['instance'].topic.positions.all()\n            self.fields['position'].queryset = positions_qs\n\n    class Meta:\n        model = TakenPosition\n        fields = ('position', 'description')\n\n    def save(self, force_insert=False, force_update=False, commit=True):\n        m = super(TakenPositionInlineModelForm, self).save(commit=False)\n        if m.position is not None:\n            m.topic = m.position.topic\n        m.save()\n        return m\n\n\nclass TakenPositionCandidateInline(admin.TabularInline):\n    model = TakenPosition\n    form = TakenPositionInlineModelForm\n    extra = 0\n    can_delete = False\n\n    def formfield_for_foreignkey(self, db_field, request, **kwargs):\n        if db_field.name == 'position':\n            pass\n        return super(TakenPositionCandidateInline, self).formfield_for_foreignkey(db_field, request, **kwargs)\n\n\nclass TakenPositionAdmin(admin.ModelAdmin):\n    form = TakenPositionModelForm\nadmin.site.register(TakenPosition, TakenPositionAdmin)\n\n\nclass TakenPositionInline(admin.TabularInline):\n    model = TakenPosition\n\n\nclass PositionAdmin(admin.ModelAdmin):\n    inlines = [TakenPositionInline, ]\n\nadmin.site.register(Position, PositionAdmin)\n\n\nclass PositionInline(admin.TabularInline):\n    model = Position\n\n\nclass TopicAdmin(admin.ModelAdmin):\n    inlines = [PositionInline, ]\n    list_display = ('__str__', 'election')\n\n    def save_model(self, request, obj, form, change):\n        creating = not change\n        obj.save()\n        if creating:\n            for candidate in obj.election.candidates.all():\n                TakenPosition.objects.get_or_create(topic=obj, person=candidate)\n\nadmin.site.register(Topic, TopicAdmin)\n\n\nclass TopicInline(admin.TabularInline):\n    model = Topic\n\n\nclass QuestionCategoryAdmin(admin.ModelAdmin):\n    inlines = [TopicInline, ]\n    list_display = ('__str__', 'election')\nadmin.site.register(QuestionCategory, QuestionCategoryAdmin)\n\n\nclass QuestionCategoryInline(admin.TabularInline):\n    model = QuestionCategory\n\n    list_display = ('__str__', 'election')\n\n\nclass CandidateModelForm(forms.ModelForm):\n    model = Candidate\n\n    def __init__(self, *args, **kwargs):\n        super(CandidateModelForm, self).__init__(*args, **kwargs)\n        self.extra_info_fields = settings.DEFAULT_CANDIDATE_EXTRA_INFO.keys()\n        for key in self.extra_info_fields:\n            self.fields.update({key: forms.CharField(max_length=512,\n                                                     label=key,\n                                                     required=False,\n                                                     widget=forms.TextInput(),\n                                                     initial=self.instance.extra_info[key]\n                                                     )})\n\n    def save(self, commit=True, *args, **kwargs):\n        instance = super(CandidateModelForm, self).save(commit, *args, **kwargs)\n        for key in self.extra_info_fields:\n            instance.extra_info[key] = self.cleaned_data.get(key, None)\n        if commit:\n            instance.save()\n\n        return instance\n\n\nclass ElectionModelForm(forms.ModelForm):\n    model = Election\n\n    def __init__(self, *args, **kwargs):\n        super(ElectionModelForm, self).__init__(*args, **kwargs)\n        self.extra_info_fields = settings.DEFAULT_ELECTION_EXTRA_INFO.keys()\n        for key in self.extra_info_fields:\n            self.fields.update({key: forms.CharField(max_length=512,\n                                                     label=key,\n                                                     required=False,\n                                                     widget=forms.TextInput(),\n                                                     initial=self.instance.extra_info[key]\n                                                     )})\n\n    def save(self, commit=True, *args, **kwargs):\n        instance = super(ElectionModelForm, self).save(commit, *args, **kwargs)\n        for key in self.extra_info_fields:\n            instance.extra_info[key] = self.cleaned_data.get(key, None)\n        if commit:\n            instance.save()\n        return instance\n\n\nclass ElectionAdmin(admin.ModelAdmin):\n    form = ElectionModelForm\n    search_fields = ['name', 'tags']\n    inlines = [QuestionCategoryInline, ]\n\n    def get_fieldsets(self, request, obj=None):\n        fieldsets = super(ElectionAdmin, self).get_fieldsets(request, obj)\n        if hasattr(request, \"_gfs_marker\"):\n            for key in settings.DEFAULT_ELECTION_EXTRA_INFO.keys():\n                fieldsets[0][1]['fields'] += (key,)\n        setattr(request, \"_gfs_marker\", 1)\n        return fieldsets\n\nadmin.site.register(Election, ElectionAdmin)\n\n\nclass OrgnizationAdmin(admin.ModelAdmin):\n    pass\nadmin.site.register(Organization, OrgnizationAdmin)\n\n\nclass ContactDetailInline(GenericTabularInline):\n    model = ContactDetail\n    fields = ('label', 'contact_type', 'value')\n\n\nclass MembershipInline(admin.TabularInline):\n    model = Membership\n    fields = ('label', 'role', 'organization', 'on_behalf_of', 'post', 'start_date', 'end_date', 'area')\n\n\nclass OtherNameInline(GenericTabularInline):\n    model = OtherName\n\n\nclass PersonalDataInline(admin.TabularInline):\n    model = PersonalData\n\n\nclass CandidateAdmin(admin.ModelAdmin):\n    form = CandidateModelForm\n    inlines = [\n        ContactDetailInline,\n        MembershipInline,\n        OtherNameInline,\n        PersonalDataInline,\n        TakenPositionCandidateInline,\n    ]\n    search_fields = ['name', 'election__name']\n    ordering = ['name']\n\n    def get_fieldsets(self, request, obj=None):\n        fieldsets = super(CandidateAdmin, self).get_fieldsets(request, obj)\n        if hasattr(request, \"_gfs_marker\"):\n            for key in settings.DEFAULT_CANDIDATE_EXTRA_INFO.keys():\n                fieldsets[0][1]['fields'] += (key,)\n        setattr(request, \"_gfs_marker\", 1)\n        return fieldsets\n\n    def save_model(self, request, obj, form, change):\n        creating = not change\n        obj.save()\n        if creating:\n            for cat in obj.election.categories.all():\n                for topic in cat.topics.all():\n                    TakenPosition.objects.get_or_create(topic=topic, person=obj)\n\nadmin.site.register(Candidate, CandidateAdmin)\n\n\nclass PostAdmin(admin.ModelAdmin):\n    pass\nadmin.site.register(Post, PostAdmin)\n\n\nclass AreaAdmin(admin.ModelAdmin):\n    pass\nadmin.site.register(Area, AreaAdmin)\n", "path": "elections/admin.py"}]}