Exemplo n.º 1
0
    def get_context_data(self, **kwargs):

        context = super().get_context_data(**kwargs)
        collection = self.kwargs.get('collection', 'Kitchen Sink')
        print(f'Getting all snippets with collection label {collection}')

        if collection == 'All Skillets':
            skillets = snippet_utils.load_all_snippets(self.app_dir)
        else:
            skillets = snippet_utils.load_snippets_by_label('collection', collection, self.app_dir)

        # Check if the skillet builder has specified an order for their Skillets
        # if so, sort them that way be default, otherwise sort by name
        order_index = 1000
        default_sort = 'name'
        for skillet in skillets:
            if 'order' not in skillet['labels']:
                skillet['labels']['order'] = order_index
                order_index += 1
            else:
                default_sort = 'order'

        context['skillets'] = skillets
        context['collection'] = collection
        context['default_sort'] = default_sort

        return context
Exemplo n.º 2
0
    def generate_dynamic_form(self):
        dynamic_form = forms.Form()

        # load all templates that report that they are a full panos configuration
        bs_templates = snippet_utils.load_snippets_by_label('template_category', 'panos_full', self.app_dir)

        choices_list = list()
        # grab each bootstrap template and construct a simple tuple with name and label, append to the list
        for bst in bs_templates:
            if bst['name'] == 'upload' or bst['name'] == 'default':
                # do not allow template names that conflict with our choices here
                # do not allow sneaky stuff!
                continue

            choice = (bst['name'], bst['label'])
            choices_list.append(choice)

        # let's sort the list by the label attribute (index 1 in the tuple)
        choices_list = sorted(choices_list, key=lambda k: k[1])
        choices_list.insert(0, ('none', 'Do not include a bootstrap'))
        choices_list.insert(1, ('bootstrap_xml', 'Use Default Bootstrap'))
        choices_list.insert(2, ('upload', 'Upload Custom Bootstrap'))

        dynamic_form.fields['custom_bootstrap'] = forms.ChoiceField(label='Custom Bootstrap',
                                                                    choices=tuple(choices_list))
        return dynamic_form
Exemplo n.º 3
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        cached_collections = cnc_utils.get_long_term_cached_value(self.app_dir, 'cached_collections')
        cached_collections_info = cnc_utils.get_long_term_cached_value(self.app_dir, 'cached_collections_info')
        if cached_collections is not None:
            context['collections'] = cached_collections
            context['collections_info'] = cached_collections_info
            return context

        # return a list of all defined collections
        collections = snippet_utils.load_all_label_values(self.app_dir, 'collection')

        # build dict of collections related to other collections (if any)
        # and a count of how many skillets are in the collection
        collections_info = dict()

        # manually create a collection called 'All'
        all_skillets = 'All Skillets'

        # get the full list of all snippets
        all_snippets = snippet_utils.load_all_snippets(self.app_dir)

        collections_info[all_skillets] = dict()
        collections_info[all_skillets]['count'] = len(all_snippets)
        collections_info[all_skillets]['related'] = list()

        # iterate over the list of collections
        for c in collections:
            if c not in collections_info:
                collections_info[c] = dict()
                collections_info[c]['count'] = 0

            skillets = snippet_utils.load_snippets_by_label('collection', c, self.app_dir)
            collections_info[c]['count'] = len(skillets)
            related = list()

            for skillet in skillets:
                if 'labels' in skillet and 'collection' in skillet['labels']:
                    if type(skillet['labels']['collection']) is list:
                        for related_collection in skillet['labels']['collection']:
                            if related_collection != c and related_collection not in related:
                                related.append(related_collection)

            collections_info[c]['related'] = json.dumps(related)

        collections.append('Kitchen Sink')
        collections.append(all_skillets)
        context['collections'] = collections
        context['collections_info'] = collections_info

        cnc_utils.set_long_term_cached_value(self.app_dir, 'cached_collections',
                                             collections, 86400, 'snippet')
        cnc_utils.set_long_term_cached_value(self.app_dir, 'cached_collections_info',
                                             collections_info, 86400, 'snippet')
        return context