diff --git a/openslides/agenda/forms.py b/openslides/agenda/forms.py index 00a48281e..5808eb441 100644 --- a/openslides/agenda/forms.py +++ b/openslides/agenda/forms.py @@ -37,7 +37,7 @@ class ItemForm(CssClassMixin, forms.ModelForm): class Meta: model = Item - exclude = ('closed', 'weight', 'related_sid') + exclude = ('closed', 'weight', 'content_type', 'object_id') class RelatedItemForm(ItemForm): @@ -46,7 +46,7 @@ class RelatedItemForm(ItemForm): """ class Meta: model = Item - exclude = ('closed', 'type', 'weight', 'related_sid', 'title', 'text') + exclude = ('closed', 'type', 'weight', 'content_type', 'object_id', 'title', 'text') class ItemOrderForm(CssClassMixin, forms.Form): diff --git a/openslides/agenda/models.py b/openslides/agenda/models.py index 41392603f..776a5721c 100644 --- a/openslides/agenda/models.py +++ b/openslides/agenda/models.py @@ -14,6 +14,8 @@ from datetime import datetime from django.db import models from django.contrib.auth.models import AnonymousUser +from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes import generic from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy, ugettext_noop, ugettext as _ @@ -87,11 +89,19 @@ class Item(MPTTModel, SlideMixin): Weight to sort the item in the agenda. """ - related_sid = models.CharField(null=True, blank=True, max_length=63) + content_type = models.ForeignKey(ContentType, null=True, blank=True) + """ + Field for generic relation to a related object. Type of the object. """ - Slide-ID to another object to show it in the agenda. - For example a motion or assignment. + object_id = models.PositiveIntegerField(null=True, blank=True) + """ + Field for generic relation to a related object. Id of the object. + """ + + content_object = generic.GenericForeignKey() + """ + Field for generic relation to a related object. General field to the related object. """ speaker_list_closed = models.BooleanField( @@ -125,53 +135,27 @@ class Item(MPTTModel, SlideMixin): if link == 'delete': return reverse('item_delete', args=[str(self.id)]) - def get_related_slide(self): - """ - Return the object at which the item points. - """ - # TODO: Rename it to 'get_related_object' - object = get_slide_from_sid(self.related_sid, element=True) - if object is None: - self.title = _('< Item for deleted slide (%s) >') % self.related_sid - self.related_sid = None - self.save() - return self - else: - return object - - def get_related_type(self): - """ - Return the type of the releated slide. - """ - return self.get_related_slide().prefix - - def print_related_type(self): - """ - Print the type of the related item. - - For use in Template - ??Why does {% trans item.print_related_type|capfirst %} not work?? - """ - return _(self.get_related_type().capitalize()) - def get_title(self): """ Return the title of this item. """ - if self.related_sid is None: + if not self.content_object: return self.title - return self.get_related_slide().get_agenda_title() + try: + return self.content_object.get_agenda_title() + except AttributeError: + raise NotImplementedError('You have to provide a get_agenda_title method on your related model.') def get_title_supplement(self): """ Return a supplement for the title. """ - if self.related_sid is None: + if not self.content_object: return '' try: - return self.get_related_slide().get_agenda_title_supplement() + return self.content_object.get_agenda_title_supplement() except AttributeError: - return '(%s)' % self.print_related_type() + raise NotImplementedError('You have to provide a get_agenda_title_supplement method on your related model.') def slide(self): """ @@ -180,11 +164,11 @@ class Item(MPTTModel, SlideMixin): There are four cases: * summary slide * list of speakers - * related slide, i. e. the slide of the related object + * related item, i. e. the slide of the related object * normal slide of the item The method returns only one of them according to the config value - 'presentation_argument' and the attribute 'related_sid'. + 'presentation_argument' and the attribute 'content_object'. """ if config['presentation_argument'] == 'summary': data = {'title': self.get_title(), @@ -198,8 +182,8 @@ class Item(MPTTModel, SlideMixin): 'item': self, 'template': 'projector/agenda_list_of_speaker.html', 'list_of_speakers': list_of_speakers} - elif self.related_sid: - data = self.get_related_slide().slide() + elif self.content_object: + data = self.content_object.slide() else: data = {'item': self, diff --git a/openslides/agenda/signals.py b/openslides/agenda/signals.py index 843bb239e..c454da57f 100644 --- a/openslides/agenda/signals.py +++ b/openslides/agenda/signals.py @@ -12,6 +12,8 @@ from datetime import datetime +from django.contrib.contenttypes.models import ContentType +from django.db.models.signals import pre_delete from django.dispatch import receiver from django import forms from django.utils.translation import ugettext_lazy, ugettext_noop, ugettext as _ @@ -111,3 +113,16 @@ def agenda_list_of_speakers(sender, **kwargs): return render_to_string('agenda/overlay_speaker_projector.html', context) return Overlay(name, get_widget_html, get_projector_html) + + +@receiver(pre_delete) +def listen_to_related_object_delete_signal(sender, instance, **kwargs): + """ + Receiver to listen whether a related item has been deleted. + """ + if hasattr(instance, 'get_agenda_title'): + for item in Item.objects.filter(content_type=ContentType.objects.get_for_model(sender), object_id=instance.pk): + item.title = '< Item for deleted slide (%s) >' % instance.get_agenda_title() + item.content_type = None + item.object_id = None + item.save() diff --git a/openslides/agenda/templates/agenda/edit.html b/openslides/agenda/templates/agenda/edit.html index f62090c3b..74886d454 100644 --- a/openslides/agenda/templates/agenda/edit.html +++ b/openslides/agenda/templates/agenda/edit.html @@ -24,9 +24,9 @@
- {% if item.related_sid %} - - {% blocktrans with type=item.get_related_type|trans name=item.get_related_slide %}Edit {{ type }} {{ name }}{% endblocktrans %} + {% if item.content_object %} + + {% blocktrans with type=item.content_type.name|trans name=item.content_object %}Edit {{ type }} {{ name }}{% endblocktrans %} {% endif %}
diff --git a/openslides/agenda/templates/agenda/view.html b/openslides/agenda/templates/agenda/view.html index 50848af14..0d7bc6087 100644 --- a/openslides/agenda/templates/agenda/view.html +++ b/openslides/agenda/templates/agenda/view.html @@ -43,10 +43,10 @@- {% if not item.related_sid %} + {% if not item.content_object %} {{ item.text|safe|linebreaks }} {% else %} - {% trans item.get_related_type %} {{ item.get_related_slide }} + {% trans item.content_type.name %} {{ item.content_object }} {% endif %}
diff --git a/openslides/agenda/views.py b/openslides/agenda/views.py index 9333de7c3..cf636a922 100644 --- a/openslides/agenda/views.py +++ b/openslides/agenda/views.py @@ -197,10 +197,11 @@ class ItemUpdate(UpdateView): success_url_name = 'item_overview' def get_form_class(self): - if self.object.related_sid is None: - return ItemForm + if self.object.content_object: + form = RelatedItemForm else: - return RelatedItemForm + form = ItemForm + return form class ItemCreate(CreateView): @@ -245,6 +246,31 @@ class ItemDelete(DeleteView): % html_strong(self.object)) +class CreateRelatedAgendaItemView(SingleObjectMixin, RedirectView): + """ + View to create and agenda item for a related object. + + This view is only for subclassing in views of related apps. You + have to define 'model = ....' + """ + permission_required = 'agenda.can_manage_agenda' + url_name = 'item_overview' + url_name_args = [] + + def get(self, request, *args, **kwargs): + """ + Set self.object to the relevant object. + """ + self.object = self.get_object() + return super(CreateRelatedAgendaItemView, self).get(request, *args, **kwargs) + + def pre_redirect(self, request, *args, **kwargs): + """ + Create the agenda item. + """ + self.item = Item.objects.create(content_object=self.object) + + class AgendaPDF(PDFView): """ Create a full agenda-PDF. diff --git a/openslides/assignment/models.py b/openslides/assignment/models.py index e4f6fa570..265843a3d 100644 --- a/openslides/assignment/models.py +++ b/openslides/assignment/models.py @@ -207,6 +207,9 @@ class Assignment(models.Model, SlideMixin): def get_agenda_title(self): return self.name + def get_agenda_title_supplement(self): + return '(%s)' % _('Assignment') + def delete(self): # Remove any Agenda-Item, which is related to this assignment. for item in Item.objects.filter(related_sid=self.sid): @@ -248,6 +251,7 @@ class Assignment(models.Model, SlideMixin): ('can_manage_assignment', ugettext_noop('Can manage assignments')), # TODO: Add plural s also to the codestring ) ordering = ('name',) + verbose_name = ugettext_noop('Assignment') register_slidemodel(Assignment) diff --git a/openslides/assignment/urls.py b/openslides/assignment/urls.py index 55ef169cb..714ddc6df 100644 --- a/openslides/assignment/urls.py +++ b/openslides/assignment/urls.py @@ -13,7 +13,7 @@ from django.conf.urls import url, patterns from openslides.assignment.views import (ViewPoll, AssignmentPDF, - AssignmentPollPDF, AssignmentPollDelete, CreateAgendaItem) + AssignmentPollPDF, AssignmentPollDelete, CreateRelatedAgendaItemView) urlpatterns = patterns('openslides.assignment.views', url(r'^$', @@ -70,8 +70,8 @@ urlpatterns = patterns('openslides.assignment.views', name='print_assignment_poll', ), - url(r'^(?P