def discover_modules():
    patterns = __flatten_patterns_tree__(urls.urlpatterns, excluded=urls.excluded)

    # Remove all existing modules
    Module.objects.all().delete()
        
    try:
        for pattern in patterns:
            module = Module(
                url=pattern['url'],
                name=pattern['name'],
                view=pattern['view'],
                multiple=False
            )
            module.save()

            module.update(set__multiple=is_module_managing_occurencies(module))
            module.reload()
    except ValidationError:
        Module.objects.all().delete()

        error_msg = 'A validation error occured during the module discovery. Please provide a name to all modules urls '
        error_msg += 'using the name argument.'
        raise ModuleError(error_msg)
        # something went wrong, delete already added modules
    except Exception, e:
        Module.objects.all().delete()
        raise e
    def _save_module_to_db(self):
        # FIXME module is not saved in the right database
        module = Module()
        module.name = "registered_module"
        module.url = "registered_module"
        module.view = "registered_module"

        module.save()