def get_context_data(self, parent_context=None): context = super().get_context_data(parent_context) def user_data(user): return { "name": user_display_name(user), "avatar_url": avatar_url(user) } user = getattr(self.request, "user", None) user_pks = {user.pk} serialized_comments = [] bound = self.form.is_bound comment_formset = self.form.formsets.get("comments") comment_forms = comment_formset.forms if comment_formset else [] for form in comment_forms: # iterate over comments to retrieve users (to get display names) and serialized versions replies = [] for reply_form in form.formsets["replies"].forms: user_pks.add(reply_form.instance.user_id) reply_data = get_serializable_data_for_fields( reply_form.instance) reply_data["deleted"] = (reply_form.cleaned_data.get( "DELETE", False) if bound else False) replies.append(reply_data) user_pks.add(form.instance.user_id) data = get_serializable_data_for_fields(form.instance) data["deleted"] = (form.cleaned_data.get("DELETE", False) if bound else False) data["resolved"] = (form.cleaned_data.get("resolved", False) if bound else form.instance.resolved_at is not None) data["replies"] = replies serialized_comments.append(data) authors = { str(user.pk): user_data(user) for user in get_user_model().objects.filter( pk__in=user_pks).select_related("wagtail_userprofile") } comments_data = { "comments": serialized_comments, "user": user.pk, "authors": authors, } context["comments_data"] = comments_data return context
def get_context(self): def user_data(user): return { 'name': user_display_name(user), 'avatar_url': avatar_url(user) } user = getattr(self.request, 'user', None) authors = {user.pk: user_data(user)} if user else {} user_pks = {user.pk} serialized_comments = [] bound = self.form.is_bound comment_formset = self.form.formsets.get('comments') comment_forms = comment_formset.forms if comment_formset else [] for form in comment_forms: # iterate over comments to retrieve users (to get display names) and serialized versions replies = [] for reply_form in form.formsets['replies'].forms: user_pks.add(reply_form.instance.user_id) reply_data = get_serializable_data_for_fields( reply_form.instance) reply_data['deleted'] = reply_form.cleaned_data.get( 'DELETE', False) if bound else False replies.append(reply_data) user_pks.add(form.instance.user_id) data = get_serializable_data_for_fields(form.instance) data['deleted'] = form.cleaned_data.get('DELETE', False) if bound else False data['resolved'] = form.cleaned_data.get( 'resolved', False) if bound else form.instance.resolved_at is not None data['replies'] = replies serialized_comments.append(data) authors = { user.pk: user_data(user) for user in get_user_model().objects.filter(pk__in=user_pks) } comments_data = { 'comments': serialized_comments, 'user': user.pk, 'authors': authors } return { 'comments_data': comments_data, }
def from_instance(cls, instance, force=False): # Make sure we're using the specific version of pages if isinstance(instance, Page): instance = instance.specific object, created = TranslatableObject.objects.get_or_create_from_instance( instance) if isinstance(instance, ClusterableModel): content_json = instance.to_json() else: serializable_data = get_serializable_data_for_fields(instance) content_json = json.dumps(serializable_data, cls=DjangoJSONEncoder) if not force: # Check if the instance has changed at all since the previous revision previous_revision = object.sources.order_by("created_at").last() if previous_revision: if json.loads(content_json) == json.loads( previous_revision.content_json): return previous_revision, False return ( cls.objects.create( object=object, specific_content_type=ContentType.objects.get_for_model( instance.__class__), locale=instance.locale, object_repr=str(instance)[:200], content_json=content_json, created_at=timezone.now(), ), True, )
def from_instance(cls, instance, force=False): object, created = TranslatableObject.objects.get_or_create_from_instance( instance) if isinstance(instance, ClusterableModel): content_json = instance.to_json() else: serializable_data = get_serializable_data_for_fields(instance) content_json = json.dumps(serializable_data, cls=DjangoJSONEncoder) if not force: # Check if the instance has changed at all since the previous revision previous_revision = object.revisions.order_by("created_at").last() if previous_revision: if json.loads(content_json) == json.loads( previous_revision.content_json): return previous_revision, False return ( cls.objects.create( object=object, locale=instance.locale, content_json=content_json, created_at=timezone.now(), ), True, )
def serializable_data(page): obj = get_serializable_data_for_fields(page) for rel in get_all_child_relations(page): rel_name = rel.get_accessor_name() children = getattr(page, rel_name).all() if hasattr(rel.related_model, 'serializable_data'): obj[rel_name] = [child.serializable_data() for child in children] else: obj[rel_name] = [ get_serializable_data_for_fields(child) for child in children ] for field in get_all_child_m2m_relations(page): children = getattr(page, field.name).all() obj[field.name] = [child.pk for child in children] return obj
def update_from_db(self): """ Retrieves the source instance from the database and updates this TranslationSource with its current contents. """ instance = self.get_source_instance() if isinstance(instance, ClusterableModel): self.content_json = instance.to_json() else: serializable_data = get_serializable_data_for_fields(instance) self.content_json = json.dumps(serializable_data, cls=DjangoJSONEncoder) self.object_repr = str(instance)[:200] self.last_updated_at = timezone.now() self.save(update_fields=['content_json', 'object_repr', 'last_updated_at']) self.refresh_segments()
def update_or_create_from_instance(cls, instance): # Make sure we're using the specific version of pages if isinstance(instance, Page): instance = instance.specific object, created = TranslatableObject.objects.get_or_create_from_instance( instance) if isinstance(instance, ClusterableModel): content_json = instance.to_json() else: serializable_data = get_serializable_data_for_fields(instance) content_json = json.dumps(serializable_data, cls=DjangoJSONEncoder) # Check if the instance has changed since the previous version source = TranslationSource.objects.filter( object_id=object.translation_key, locale_id=instance.locale_id).first() # Check if the instance has changed at all since the previous version if source: if json.loads(content_json) == json.loads(source.content_json): return source, False source, created = cls.objects.update_or_create( object=object, locale=instance.locale, # You can't update the content type of a source. So if this happens, # it'll try and create a new source and crash (can't have more than # one source per object/locale) specific_content_type=ContentType.objects.get_for_model( instance.__class__), defaults={ 'locale': instance.locale, 'object_repr': str(instance)[:200], 'content_json': content_json, 'last_updated_at': timezone.now(), }) source.refresh_segments() return source, created
def serializable_data(self): return get_serializable_data_for_fields(self)