Ejemplo n.º 1
0
 def modify_item_values(self, item):
     item.__dict__.update(item.obj.__dict__)
     logger.debug(
         "checklist item status: {name} (display_status)".format(
             name=item.name.encode("utf-8"), display_status=item.display_status
         )
     )
Ejemplo n.º 2
0
    def append_todo_obj(self, todos):
        """ Append obj to the todo item """
        slugs = self.todos_by_slug(todos)
        # get a set of todo items in the database
        # and append them to the item object
        for db_item in self.db_todos():

            if db_item.is_deleted is True and db_item.slug in slugs:
                del slugs[db_item.slug]
                self.delete_item(db_item)
                logger.debug("Deleted the todolist slug: {slug}".format(slug=db_item.slug))
            else:

                try:
                    # index exists so we have them already so append this key
                    slugs[db_item.slug].obj = db_item
                    self.modify_item_values(slugs[db_item.slug])
                except KeyError:
                    # if the index does not exist, it means its
                    # a custom created item that the user has added
                    slugs[db_item.slug] = db_item
                    slugs[db_item.slug].obj = db_item

                    self.todos_by_cat[db_item.category] = self.todos_by_cat.get(db_item.category, [])

                    self.todos_by_cat[db_item.category].append(slugs[db_item.slug])

        # return the actual items and not our temp dictionary
        return slugs.values()
Ejemplo n.º 3
0
    def bulk_create(self):
        logger.debug("start bulk_create")

        todo_list = []
        db_todo_slugs = [t.slug for t in self.db_todos()]

        for t in self.todos:
            if type(t) == Bunch and hasattr(t, "toDict"):
                t = t.toDict()
                t.pop("attachment", None)
                t.pop("has_attachment", None)
                t.pop("num_attachments", None)
                t.pop("group", None)
                t.pop("note", None)
                if t.get("slug") is not None and t.get("slug") not in db_todo_slugs:

                    todo = self.todo_data_defaults(ToDo(**t))

                    todo_list.append(todo)

        if todo_list:
            ToDo.objects.bulk_create(todo_list)
Ejemplo n.º 4
0
    def extract_repeater_values(self, items):
        cleaned_items = []
        if items is None:
            logger.error('items passed into JavascriptRegionCloneMixin is None')
        else:
            for key, item_value in sorted(items.items()):
                id = item_value.get('id')
                val = item_value.get('val')

                match = RE_FIND_END_NUMERAL.search(key)
                if match is None:
                    # this is the primary first item (as it has no *_<int>)
                    index = 0
                else:
                    # these are secondary items (as they have *_<int>)
                    # remove the _ and convert to integer
                    index = int(match.group().replace('_', ''))

                # remove the _<int> from the id
                # if it exists
                match = RE_FIND_END_NUMERAL.search(id)
                if match is not None:
                    id = key.replace(match.group(), '')

                logger.debug('\n{index} - {id}: {val}\n\n'.format(index=index,
                                                                  id=id,
                                                                  val=val))

                try:
                    obj = cleaned_items[index]
                    obj[id] = val
                    cleaned_items[index] = obj
                    logger.debug("updated new item {obj}\n".format(obj=obj))
                except IndexError:
                    obj = {}
                    obj[id] = val
                    cleaned_items.insert(index, obj)
                    logger.debug("inserted new item {obj}\n".format(obj=obj))

                logger.debug("cleaned_items: {cleaned_items}\n".format(cleaned_items=cleaned_items))

        return cleaned_items
Ejemplo n.º 5
0
    def handle_repeater(self, item):
        repeater_key = None
        num_items = 0
        # does the item object contain a repeater_key
        if hasattr(item, 'repeater_key'):
            repeater_key = item.repeater_key
            # singular reference for verbage; i.e. Founder(<-singular value) #1
            singular = getattr(item, 'singular', None)

            logger.debug('Found repeater_key: %s' % repeater_key)

            # get the repeater_key values from the project_data
            items = self.project_data.get(repeater_key, None)

            # ok, we have no data form comapny_data based on the repeater_key
            # therefor we need to see if we can extract the data from an attribute/method
            # available on the project_data ProjectIntakeFormIsCompleteBunch object
            if not items and hasattr(self.project_data, repeater_key):
                items = getattr(self.project_data, repeater_key)

            # do we have items yet? great now we need to see if its a list
            # or an int, ie do we have a set of elements saved in the project_data
            # or is it a simple int value ie. num_officers which then allows us to generate
            if items:
                has_extra_dict = False
                # handle being passed a [] or (), otherwise it should be an int
                if type(items) in [dict]:
                    # if were pased a dict of repeater items
                    has_extra_dict = True
                    items = self.parse_repeater_dict(items=items)
                    num_items = len(items)
                    logger.debug('Created {num_items} from dict data'.format(num_items=num_items))

                elif type(items) in [list]:
                    num_items = len(items)
                    logger.debug('Created {num_items} from list data'.format(num_items=num_items))

                else:
                    # should be an int
                    num_items = int(items)
                    items = [i for i in xrange(1, num_items + 1)]
                    logger.debug('Created {num_items} from xrange'.format(num_items=num_items))

            logger.debug('All Items repeater_key: %s items: %s' % (repeater_key, items,))

            cloned_list = item.checklist[:]
            item.checklist = []

            if num_items > 0:
                # need to repeat this segment X times by
                # field_name specified

                for num in xrange(0, num_items):
                    # contextualize and replace the textual values
                    display_num = num + 1
                    logger.debug('Creating repeater {repeater_key}, {num}/{display_num}'.format(repeater_key=repeater_key, num=num, display_num=display_num))

                    # set the extra dict to be passed in if it exists
                    extra = items[num] if has_extra_dict is True else {}

                    # append to the item.checklist
                    # account for the extra dict which could contain region-clone key->vals
                    generic_name = '{name} #{num}'.format(name=singular,
                                                          num=display_num)

                    # update the yml line items with the context provided here
                    # by the kwargs
                    item.checklist += [self.item_context(item=copy.deepcopy(cloned_item),
                                                        num=display_num,
                                                        generic_name=generic_name,
                                                        **extra) for cloned_item in cloned_list]