Example #1
0
    def folders(self, box_provider, parent={'id': 0}):
        token = box_provider.tokens

        f = Folders(token=token)
        folders = f.get(id=0, parent=parent)

        return [f for f in folders.get('item_collection', {}).get('entries', []) if f.get('type') == 'folder']
Example #2
0
    def handle(self, *args, **options):
        self.slugs = args
        self.options = options

        try:
            args[0]
        except IndexError:
            raise CommandError('You must specify at least 1 matter slug')

        for m in self.matters:

            matter_name = _safe_matter_name(m)
            logger.info('Sync to Box: %s' % m.name)

            for p in self.participants(matter=m):

                logger.info('Participant: %s' % p)

                box_provider = p.social_auth.filter(provider=self.provider).first()

                if box_provider:

                    current_folders = self.current_folders(box_provider=box_provider)
                    token = self.valid_token(box_provider=box_provider)
                    logger.info('TOKEN: %s' % token)

                    f = Folders(token=token)

                    if matter_name not in current_folders.keys():
                        # does not exist, so create
                        resp = f.post(name=matter_name, parent={'id': 0})
                        current_folders = self.current_folders(box_provider=box_provider)

                    matter_folder_id = self.matter_folder_id(name=matter_name, current_folders=current_folders)

                    # create revision files, according to user class
                    for item in m.item_set.all():
                        category_folder_id = matter_folder_id

                        # Ensure Category
                        if item.category is not None:
                            resp = f.post(name=slugify(item.category), parent={'id': matter_folder_id})
                            try:
                                category_folder_id = resp.get('id') if resp.get('id', None) is not None else f.response.json().get('context_info', {}).get('conflicts',[])[0].get('id', None)
                            except IndexError:
                               category_folder_id = matter_folder_id

                        # Create item folder
                        resp = f.post(name=slugify(item.name), parent={'id': category_folder_id})
                        item_folder_id = resp.get('id') if resp.get('id', None) is not None else f.response.json().get('context_info', {}).get('conflicts',[])[0].get('id', None)
                        
                        logger.info('matter: %s parent: %s' % (matter_folder_id, category_folder_id))

                        #for rev in item.revision_set.all():
                        if item.latest_revision:
                            for rev in [item.latest_revision]:
                                rev.ensure_file()
                                target_file = rev.executed_file
                                fl = UploadFiles(token=token, sha1=_file_sha1(target_file=target_file))
                                logger.info('uploading with parent: %s' % (item_folder_id,))
                                resp = fl.post(files={'file': default_storage.open(target_file)}, parent_id=item_folder_id)
                                logger.debug(fl.response.content)

                        # attachments
                        attachments_list = item.attachments.all()
                        # only create teh folder if we have attachments
                        if attachments_list:
                            resp = f.post(name='attachments', parent={'id': item_folder_id})                        
                            attachments_folder_id = resp.get('id') if resp.get('id', None) is not None else f.response.json().get('context_info', {}).get('conflicts',[])[0].get('id', None)
                            # update the files
                            for attachment in attachments_list:
                                target_file = attachment.attachment
                                fl = UploadFiles(token=token, sha1=_file_sha1(target_file=target_file))
                                resp = fl.post(files={'file': default_storage.open(target_file)}, parent_id=attachments_folder_id)