This repository has been archived on 2020-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
photo/photo/view.py

90 lines
2.5 KiB
Python

# -*- coding: UTF-8 -*-
import json
from django.shortcuts import render
from django.http import HttpResponse
from photo.util import fileUtil, properties
import os
prop = properties.properties(os.path.abspath("photo.properties")).get_properties()
def photo(request):
path = prop['img.url']
fold = ""
if request.method == "GET":
fold = request.GET.get("name", "")
if request.method == "POST":
fold = request.POST.get("name", "")
new_path = path + "/" + fold
context = {'index': 'Hello World!', 'name': fold}
info = fileUtil.read_file_json(new_path + '/info.json')
if 'name' in info:
context['title'] = info['name']
return render(request, 'photo/index.html', context)
def get_pics(path, fold=""):
pics = fileUtil.get_all_pics(path)
data = []
for pic in pics:
if fold != "":
tmp_pic = {'name': 'img/' + fold + "/" + pic, 'caption': ''}
else:
tmp_pic = {'name': 'img/' + pic, 'caption': ''}
data.append(tmp_pic)
if len(data) >= 2:
data.append(data[1])
return data
def photo_all(request):
path = prop['img.url']
data = []
folds = fileUtil.get_all_folds(path)
folds.append({'fold': '', 'fold_path': path})
for fold in folds:
pics = fileUtil.get_all_pics(fold['fold_path'])
if len(pics) == 0:
continue
info = fileUtil.read_file_json(fold['fold_path'] + '/info.json')
entity = {'name': info['name'], 'des': info['des'],
'fold': fold['fold']}
if fold['fold'] == '':
entity['pic'] = 'img/' + pics[0]
else:
entity['pic'] = 'img/' + fold['fold'] + '/' + pics[0]
data.append(entity)
context = {'data': data, 'success': 'true'}
print(context)
# return HttpResponse(json.dumps(data), content_type="application/json")
return render(request, 'photo/photos.html', context)
def photo_qry(request):
path = prop['img.url']
fold = ""
if request.method == "GET":
fold = request.GET.get("name", "")
if request.method == "POST":
fold = request.POST.get("name", "")
new_path = ""
data = []
if fold != "":
new_path = path + "/" + fold
data = get_pics(new_path, fold=fold)
if len(data) == 0:
if fold != "":
print("fold %s has none pics" % fold)
data = get_pics(path)
# print(data)
return HttpResponse(json.dumps(data), content_type="application/json")