feat: 完成龙虾记忆同步系统
后端: - Django + DRF - PostgreSQL 数据库 - 文件扫描服务 - 差异检查服务 - 完整 REST API 前端: - React + Ant Design - 文件树展示 - 差异对比 - API 客户端封装 部署: - Docker Compose - 后端 Dockerfile - 前端 Dockerfile - 一键启动脚本 功能: - 扫描龙虾记忆文件 - 检查文件差异 - 双向同步(本地 <-> 数据库) - 版本历史 - 统计信息
This commit is contained in:
61
backend/memory_app/models.py
Normal file
61
backend/memory_app/models.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from django.db import models
|
||||
from django.core.validators import FileExtensionValidator
|
||||
import hashlib
|
||||
|
||||
|
||||
class LobsterMemory(models.Model):
|
||||
"""龙虾记忆文件模型"""
|
||||
|
||||
STATUS_CHOICES = [
|
||||
('consistent', '一致'),
|
||||
('local_newer', '本地更新'),
|
||||
('db_newer', '数据库更新'),
|
||||
('conflict', '冲突'),
|
||||
]
|
||||
|
||||
lobster_id = models.CharField(max_length=50, help_text='龙虾ID')
|
||||
|
||||
file_path = models.CharField(max_length=500, help_text='文件相对路径')
|
||||
|
||||
content = models.TextField(help_text='文件内容')
|
||||
|
||||
hash = models.CharField(max_length=64, help_text='SHA256哈希')
|
||||
|
||||
status = models.CharField(
|
||||
max_length=20,
|
||||
choices=STATUS_CHOICES,
|
||||
default='consistent',
|
||||
help_text='同步状态'
|
||||
)
|
||||
|
||||
version = models.IntegerField(default=1, help_text='版本号')
|
||||
|
||||
size = models.IntegerField(default=0, help_text='文件大小(字节)')
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True, help_text='创建时间')
|
||||
|
||||
updated_at = models.DateTimeField(auto_now=True, help_text='更新时间')
|
||||
|
||||
class Meta:
|
||||
db_table = 'lobster_memory'
|
||||
unique_together = ('lobster_id', 'file_path', 'version')
|
||||
ordering = ['-updated_at']
|
||||
indexes = [
|
||||
models.Index(fields=['lobster_id', 'file_path']),
|
||||
models.Index(fields=['status']),
|
||||
models.Index(fields=['updated_at']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.lobster_id}/{self.file_path} (v{self.version})"
|
||||
|
||||
def compute_hash(self, content):
|
||||
"""计算SHA256哈希"""
|
||||
return hashlib.sha256(content.encode('utf-8')).hexdigest()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""保存时自动计算哈希和大小"""
|
||||
if self.content:
|
||||
self.hash = self.compute_hash(self.content)
|
||||
self.size = len(self.content.encode('utf-8'))
|
||||
super().save(*args, **kwargs)
|
||||
Reference in New Issue
Block a user