Update code-feature view to use qr std

This commit is contained in:
Fam Zheng 2025-05-14 22:18:19 +01:00
parent 412485b45b
commit 38ab815ba2
2 changed files with 17 additions and 64 deletions

View File

@ -535,12 +535,10 @@ def http405():
r.status_code = 405
return r
def code_to_roi_key(code):
def code_to_qr_std_key(code):
code = str(code)
a = code[0:2] or "_"
b = code[0:4] or "_"
key = f"features_v1/{a}/{b}/{code}.jpg"
return key
pref = code[:2]
return f'v5/{pref}/{code}.jpg'
class BaseView(View):
name = ''
@ -579,11 +577,8 @@ class BaseView(View):
return http401()
return super().dispatch(request, *args, **kwargs)
def get_feature_roi(self, code):
return oss_get(code_to_roi_key(code), bucket=settings.FEATURES_BUCKET)
def save_feature_roi(self, code, roi):
return oss_put(code_to_roi_key(code), roi, bucket=settings.FEATURES_BUCKET)
def get_qr_std(self, code):
return oss_get(code_to_qr_std_key(code), bucket=settings.OSS_QRS_BUCKET)
class UserInfoView(BaseView):
name = 'userinfo'
@ -1545,24 +1540,17 @@ class ScanStatsByProvinceView(BaseView):
return JsonResponse(ret)
class CodeFeatureRoiView(BaseView):
name = 'code-feature-roi'
class QrStdView(BaseView):
name = 'qr-std'
auth_check = 'admin'
def get(self, request):
c = request.GET.get("code")
feature = self.get_feature_roi(c)
if feature:
return HttpResponse(feature, content_type="image/jpeg")
qr = self.get_qr_std(c)
if qr:
return HttpResponse(qr, content_type="image/jpeg")
return http404()
def post(self, request):
for i, feature in request.FILES.items():
name = request.FILES[i].name
c = name.split('/')[-1].split('.')[0]
self.save_feature_roi(c, feature.read())
return JsonResponse({})
def parse_estor_params(headers):
ret = {}
for k, v in headers.items():
@ -1575,40 +1563,6 @@ def parse_estor_params(headers):
ret[k] = v
return ret
class EstorFeatureRoi(BaseView):
name = 'estor-feature-roi'
auth_check = None
def post(self, request):
rh = parse_estor_params(request.META)
path = rh.get('HTTP_X_ESTOR_PATH')
if not path:
return http400("X-Estor-Path not set")
token = rh.get('HTTP_X_ESTOR_PARAM_EMBLEM_TOKEN')
t = AuthToken.objects.filter(token=token).first()
if not t or not t.admin:
return http401()
overwrite = rh.get('HTTP_X_ESTOR_PARAM_OVERWRITE') == "yes"
fail_if_exists = rh.get('HTTP_X_ESTOR_PARAM_FAIL_IF_EXISTS') == "yes"
code = os.path.basename(path).split(".", maxsplit=1)[0]
if not code:
return http400("code invalid")
if fail_if_exists or overwrite:
exists = oss_has(code_to_roi_key(code))
if exists:
if fail_if_exists:
return http400("code feature roi already existing")
if not overwrite:
return JsonResponse({
"saved": 0,
})
self.save_feature_roi(code, request.body)
return JsonResponse({
"saved": 1,
})
class QrStdUploadView(BaseView):
name = 'qr-std-upload'
auth_check = None
@ -1634,9 +1588,8 @@ class QrStdUploadView(BaseView):
raise Exception("file too large")
if len(name) < 2:
raise Exception("invalid file name")
pref = name[:2]
key = f"v5/{pref}/{fname}"
r = oss_put(key, body, bucket=settings.OSS_QRS_BUCKET, endpoint=settings.OSS_QRS_ENDPOINT)
key = code_to_qr_std_key(name)
oss_put(key, body, bucket=settings.OSS_QRS_BUCKET, endpoint=settings.OSS_QRS_ENDPOINT)
class EstorArchive(BaseView):
name = 'estor-archive'
@ -1734,12 +1687,12 @@ class GlobalConfigView(BaseView):
r.save()
return JsonResponse({"ok": True, "id": r.pk})
class FeatureStatView(BaseView):
name = 'feature-stat'
class QrStdStatView(BaseView):
name = 'qr-std-stat'
auth_check = 'admin'
def get(self, request):
ret = oss_stat(bucket=settings.FEATURES_BUCKET)
ret = oss_stat(bucket=settings.OSS_QRS_BUCKET)
return JsonResponse(ret)
class LogReportView(BaseView):

View File

@ -66,7 +66,7 @@ export default {
this.search_result = null;
this.search_error = null;
try {
var url = "/api/v1/code-feature-roi/?token=" + this.$root.token + "&code=" + this.search_code;
var url = "/api/v1/qr-std/?token=" + this.$root.token + "&code=" + this.search_code;
var r = await this.$root.api_get(url);
if (r) {
this.search_result = url;
@ -80,7 +80,7 @@ export default {
},
async reload() {
this.loading = true;
var r = await this.$root.api_get("/api/v1/feature-stat/");
var r = await this.$root.api_get("/api/v1/qr-std-stat/");
this.objects = r.data.objects;
this.size = r.data.size;
this.loading = false;