コード例 #1
0
ファイル: views.py プロジェクト: ikicic/skoljka
def folder_my(request):
    """
        List all folders created by the user.
    """
    folders = list(Folder.objects.filter(author_id=request.user.id))
    if not folders:
        return {}

    original_ids = set(x.id for x in folders)

    data = get_visible_folder_tree(folders, request.user) or {}
    ancestor_ids = data.get('ancestor_ids', [])
    sorted_folders = data.get('sorted_folders', [])

    accessible_ids = set()

    folders_html = []
    for x in sorted_folders:
        if x.id in original_ids:
            html = x._html_menu_item(True, x._depth, None,
                cls='folder-list-my',
                extra='<a href="/folder/{}/edit/" class="folder-list-my-edit"' \
                    ' title="Uredi"> <i class="icon-edit"></i></a>'.format(x.id))
            accessible_ids.add(x.id)
        else:
            html = x._html_menu_item(x.id in ancestor_ids, x._depth, None)
        folders_html.append(html)

    return {
        'folders_html': u''.join(folders_html),
        'inaccessible_folders': [x for x in folders
            if x.parent_id and x.id not in accessible_ids],
    }
コード例 #2
0
ファイル: forms.py プロジェクト: ikicic/skoljka
    def _prepare_parent_field(self):
        if self.instance.id is not None and self.instance.parent_id is None:
            del self.fields['parent']
            return

        # User can put his folder anywhere he wants (into any visible folder),
        # but that doesn't mean he can make it public!

        # Check all permissions. Remove inaccessible folders.
        # WARNING: Remove the subtree of the given folder!
        data = get_visible_folder_tree(Folder.objects.filter(editable=True),
            self.user, exclude_subtree=self.instance)

        # WARNING: Do not forget to remove self.instance from the list!
        exclude_id = self.instance.id if self.instance else None

        # Keep only editable folders (maybe some folders are in the tree, but
        # not editable). Convert to choice pairs.
        root = Folder.objects.get(parent_id__isnull=True)
        root._depth = 0
        self._parent_choices = [root]
        self._parent_choices.extend(filter(
            lambda x: x.editable and x.id != exclude_id, data['sorted_folders']
        ))
        choices = [(x.id, '-- ' * (x._depth - 1) + x.name)
            for x in self._parent_choices]

        # Make sure initial parent exist and it's accessible
        self.initial_parent = next(
            (x for x in self._parent_choices if self.initial_parent_id == x.id),
            None,
        )

        if not self.initial_parent:
            self.initial_parent_id = None

        # If parent is inaccessible, notify user.
        if self.instance and self.instance.parent_id:
            parent_ok = next((x for x in self._parent_choices
                if self.instance.parent_id == x.id), None)

            if not parent_ok:
                # hackish, is there any more formal way to do this?
                if 'parent' not in self.errors:
                    self.errors.update(parent=[])
                self.errors['parent'].append('Trenutačna roditeljska kolekcija '
                    'nije dostupna! Kako bi ova kolekcija bila dostupna iz '
                    'izbornika, premjestite je u neku dostupnu kolekciju.')

        self.fields['parent'] = forms.ChoiceField(
            choices=choices, label='Roditelj', initial=self.initial_parent_id)