def sync_assets_from_file_system()

in gnm_deliverables/models.py [0:0]


    def sync_assets_from_file_system(self):
        """
        performs a scan of the drop-folder associated with this deliverable. If a file is found that does not correspond
        to a DeliverableAsset record then create a record for it, if there is a corresponding record then update the ctime/
        mtime/atime.
        Also, if any asset records have NOT been imported (i.e. their type and item_id is null) and their corresponding
        files have been removed then delete those records.
        :return: a dictionary with two keys, a count of "added" records and a count of "removed" records.
        """
        assets_on_fs = []
        added_count = 0
        removed_count = 0

        for f in find_files_for_deliverable(self.name):
            asset, created = DeliverableAsset.objects.get_or_create(
                filename=f.path,
                deliverable=self,
                size=f.size,
                modified_dt=f.modified_dt,
                defaults=dict(
                    access_dt=f.access_dt,
                    changed_dt=f.changed_dt,
                    absolute_path=f.absolute_path
                )
            )
            assets_on_fs.append(asset)
            if created:
                logger.info('Asset created: %s' % asset)
                added_count += 1
            else:
                logger.info('Asset already existed: %s' % asset)
                # Update defaults
                asset.access_dt = f.access_dt
                asset.changed_dt = f.changed_dt
                asset.absolute_path = f.absolute_path
                asset.save()

        # Remove assets rows that are not found on the FS and does not have an item tied to it
        assets_to_delete = self.assets.filter(
            type__isnull=True,
            online_item_id__isnull=True
        ).exclude(
            id__in=[a.id for a in assets_on_fs]
        )
        delete_count = assets_to_delete.count()
        if delete_count > 0:
            assets_to_delete.delete()
            removed_count += 1
            logger.info('Deleted %s asset rows' % delete_count)
        return {"added": added_count, "removed": removed_count}