Optimize LoRA cache sorting with flexible resort method

- Add optional name-only sorting to LoRA cache resort method
- Update API routes to use new resort method with name-only flag
- Refactor HTML template to improve script placement and initialization check
This commit is contained in:
Will Miao
2025-02-04 20:02:59 +08:00
parent 7fb7e2aa2e
commit 763e55fcae
3 changed files with 10 additions and 9 deletions

View File

@@ -368,7 +368,7 @@ class ApiRoutes:
logger.error(f"Error fetching CivitAI data for {lora['file_path']}: {e}")
if needs_resort:
cache.sorted_by_name = sorted(cache.raw_data, key=itemgetter('model_name'))
await cache.resort(name_only=True)
# 发送完成消息
await ws_manager.broadcast({

View File

@@ -14,18 +14,19 @@ class LoraCache:
def __post_init__(self):
self._lock = asyncio.Lock()
async def resort(self):
async def resort(self, name_only: bool = False):
"""Resort all cached data views"""
async with self._lock:
self.sorted_by_name = sorted(
self.raw_data,
key=lambda x: x['model_name'].lower() # Case-insensitive sort
)
self.sorted_by_date = sorted(
self.raw_data,
key=itemgetter('modified'),
reverse=True
)
if not name_only:
self.sorted_by_date = sorted(
self.raw_data,
key=itemgetter('modified'),
reverse=True
)
# Update folder list
self.folders = sorted(list(set(
l['folder'] for l in self.raw_data

View File

@@ -60,9 +60,9 @@
</div>
<script type="module" src="/loras_static/js/main.js"></script>
{% if is_initializing %}
<script>
// 检查初始化状态并设置自动刷新
{% if is_initializing %}
async function checkInitStatus() {
try {
const response = await fetch('/api/loras?page=1&page_size=1');
@@ -81,7 +81,7 @@
// 启动状态检查
checkInitStatus();
{% endif %}
</script>
{% endif %}
</body>
</html>