def __iter__(self): self.next_id = (Comment.objects.all().aggregate(max_id=Max('id'))['max_id'] or 0) + 1 for model in self.GENERATE_FOR_MODELS: model_class = apps.get_model(model) ctype = ContentType.objects.get_for_model(model_class) for instance in get_default_manager(model_class).all(): root_comment = Comment( parent=None, level=0, content_type=ctype, object_id=instance.pk, original_comment='html:', filtered_comment='', user_name='', created=instance.created, updated=instance.created, ) root_comment.id = self.next_id root_comment.tree_id = self.next_tree_id self.next_id += 1 self.next_tree_id += 1 tree = self.generate_tree(parent_id=root_comment.id, lft=2, level=root_comment.level) # pylint: disable=no-member root_comment.lft = 1 root_comment.rght = 2 + len(tree) * 2 yield root_comment for comment in tree: comment.content_type = ctype comment.object_id = instance.pk comment.filtered_comment = comment.original_comment.field_text comment.updated = comment.created comment.tree_id = root_comment.tree_id yield comment
def unique_slugify(self, instance, **kwargs): slug = getattr(instance, self.name) if not slug: if self.title_field: slug = slugify(getattr(instance, self.title_field)) else: return if not slug: slug = '-' slug_field = get_meta(instance).get_field(self.name) slug_length = slug_field.max_length slug = slug[:slug_length - self.reserve_chars] queryset = get_default_manager(instance).all() if instance.pk: queryset = queryset.exclude(pk=instance.pk) slug_field_query = self.name + '__startswith' filter_fields = dict([(f, getattr(instance, f)) for f in self.filter_fields]) filter_fields[slug_field_query] = slug all_slugs = set(queryset.filter(**filter_fields).values_list(self.name, flat=True)) max_val = 10 ** (self.reserve_chars - 1) - 1 setattr(instance, self.name, self.create_unique_slug(slug, all_slugs, max_val))
def done(self): for model in self.GENERATE_FOR_MODELS: model_class = apps.get_model(model) ctype = ContentType.objects.get_for_model(model_class) for instance in get_default_manager(model_class).all(): root = Comment.objects.get_or_create_root_comment(ctype, instance.pk)[0] update_comments_header(Comment, instance=root)
def done(self): for model in self.GENERATE_FOR_MODELS: model_class = apps.get_model(model) ctype = ContentType.objects.get_for_model(model_class) for instance in get_default_manager(model_class).all(): root = Comment.objects.get_or_create_root_comment(ctype, instance.pk)[0] update_comments_header(Comment, instance=root) if self.command is not None and self.command.verbosity > 1: self.command.stdout.write('#', ending='')
def clean_fields(self, exclude=None): if self.email: qs = get_default_manager(self).filter(email=self.email).exclude( pk=self.pk) if qs.exists(): raise ValidationError({ 'email': ['Používateľ s touto e-mailovou adresou už existuje'] }) super(User, self).clean_fields(exclude)
def done(self): for model in self.GENERATE_FOR_MODELS: model_class = apps.get_model(model) ctype = ContentType.objects.get_for_model(model_class) for instance in get_default_manager(model_class).all(): root = Comment.objects.get_or_create_root_comment( ctype, instance.pk)[0] update_comments_header(Comment, instance=root) if self.command is not None and self.command.verbosity > 1: self.command.stdout.write('#', ending='')
def __iter__(self): for model in self.GENERATE_FOR_MODELS: model_class = apps.get_model(model) ctype = ContentType.objects.get_for_model(model_class) for instance in get_default_manager(model_class).all(): Comment.objects.get_or_create_root_comment(ctype, instance.pk) self.next_id = (Comment.objects.aggregate(max_id=Max('id'))['max_id'] or 0) + 1 for model in self.GENERATE_FOR_MODELS: model_class = apps.get_model(model) ctype = ContentType.objects.get_for_model(model_class) for instance in get_default_manager(model_class).all(): root_comment = Comment.objects.get_or_create_root_comment(ctype, instance.pk)[0] tree = self.generate_tree(parent_id=root_comment.id, lft=root_comment.rght, level=root_comment.level) for comment in tree: comment.content_type = ctype comment.object_id = instance.pk comment.filtered_comment = comment.original_comment[1] comment.updated = comment.submit_date comment.tree_id = root_comment.tree_id yield comment root_comment.rght = root_comment.rght + len(tree) * 2 root_comment.save()
def resolve_content_objects(content_object_list): object_list_by_content = {} for obj in content_object_list: object_list_by_content.setdefault(obj[0], []) object_list_by_content[obj[0]].append(obj[1]) content_types = {obj.id: obj for obj in ContentType.objects.filter(pk__in=object_list_by_content.keys())} for content_type, content_object_ids in object_list_by_content.items(): object_list_by_content[content_type] = (get_default_manager(content_types[content_type].model_class()) .filter(pk__in=content_object_ids)) objects_idx = {} for content_type, content_objects in object_list_by_content.items(): for content_object in content_objects: objects_idx[(content_type, content_object.pk)] = content_object object_list = [objects_idx.get((o[0], int(o[1]))) for o in content_object_list] return object_list
def __iter__(self): self.next_id = (Comment.objects.aggregate(max_id=Max('id'))['max_id'] or 0) + 1 for model in self.GENERATE_FOR_MODELS: model_class = apps.get_model(model) ctype = ContentType.objects.get_for_model(model_class) for instance in get_default_manager(model_class).all(): submit_date = ( getattr(instance, 'created', None) or getattr(instance, 'pub_time', None) or getattr(instance, 'active_from') ) root_comment = Comment( parent=None, level=0, content_type=ctype, object_id=instance.pk, original_comment=('html', ''), filtered_comment='', user_name='', submit_date=submit_date, updated=submit_date, ) root_comment.id = self.next_id root_comment.tree_id = self.next_tree_id self.next_id += 1 self.next_tree_id += 1 tree = self.generate_tree(parent_id=root_comment.id, lft=2, level=root_comment.level) # pylint: disable=no-member root_comment.lft = 1 root_comment.rght = 2 + len(tree) * 2 yield root_comment for comment in tree: comment.content_type = ctype comment.object_id = instance.pk comment.filtered_comment = comment.original_comment[1] comment.updated = comment.submit_date comment.tree_id = root_comment.tree_id yield comment
def __iter__(self): self.next_id = (Comment.objects.all().aggregate( max_id=Max('id'))['max_id'] or 0) + 1 for model in self.GENERATE_FOR_MODELS: model_class = apps.get_model(model) ctype = ContentType.objects.get_for_model(model_class) for instance in get_default_manager(model_class).all(): root_comment = Comment( parent=None, level=0, content_type=ctype, object_id=instance.pk, original_comment='html:', filtered_comment='', user_name='', created=instance.created, updated=instance.created, ) root_comment.id = self.next_id root_comment.tree_id = self.next_tree_id self.next_id += 1 self.next_tree_id += 1 tree = self.generate_tree(parent_id=root_comment.id, lft=2, level=root_comment.level) # pylint: disable=no-member root_comment.lft = 1 root_comment.rght = 2 + len(tree) * 2 yield root_comment for comment in tree: comment.content_type = ctype comment.object_id = instance.pk comment.filtered_comment = comment.original_comment comment.updated = comment.created comment.tree_id = root_comment.tree_id yield comment
def clean_fields(self, exclude=None): if self.email: qs = get_default_manager(self).filter(email=self.email).exclude(pk=self.pk) if qs.exists(): raise ValidationError({'email': ['Používateľ s touto e-mailovou adresou už existuje']}) super(User, self).clean_fields(exclude)
def clean_fields(self, exclude=None): if self.email: qs = get_default_manager(self).filter(email=self.email).exclude(pk=self.pk) if qs.exists(): raise ValidationError({'email': [self.unique_error_message(self.__class__, ['email'])]}) super(User, self).clean_fields(exclude)
def post_comment(request): data = request.POST.copy() if request.user.is_authenticated(): data['email'] = request.user.email if request.user.get_full_name(): data['name'] = request.user.get_full_name() else: data['name'] = request.user.username else: data['email'] = '*****@*****.**' if not data['content_type'] or not data['object_id'] or not data['parent_pk']: return http.HttpResponseBadRequest() model = models.get_model(*data['content_type'].split(".", 1)) target = get_default_manager(model).get(pk = data['object_id']) parent = Comment.all_comments.get(pk = data['parent_pk']) content_object = parent.content_object form = get_form()(target, logged = request.user.is_authenticated(), parent_comment = parent, data = data, files = request.FILES) if form.security_errors(): return http.HttpResponseBadRequest() if form.errors or not 'create' in data or parent.is_locked: template_list = [ "comments/{0}_{1}_preview.html".format(get_meta(model).app_label, get_meta(model).module_name), "comments/{0}_preview.html".format(get_meta(model).app_label), "comments/preview.html", ] valid = not form.errors comment = form.get_comment() if request.user.is_authenticated(): comment.user = request.user context = { 'next': data['next'], 'comment': comment, 'valid': valid, 'parent': parent if parent.parent_id else False, 'content_object': content_object, 'module_name': get_module_name(content_object), 'module_url': get_module_url(content_object), 'form': form, 'attachments': form.get_attachments(), } if parent.is_locked: del context['form'] del context['attachments'] return TemplateResponse(request, "comments/error.html", context) return TemplateResponse(request, template_list, context) comment = form.get_comment_object() comment.ip_address = request.META.get("REMOTE_ADDR", None) if request.user.is_authenticated(): comment.user = request.user comment.save() form.move_attachments(comment) data['next'] = data['next'] + '#link_' + str(comment.pk) return HttpResponseRedirect(data['next'])