def get_by_name(name, force=False): if not force: Character.check_cache() if not force and name in Character.cache: return Character.cache[name] else: record = CharacterTable().get_by_name(name) if record: c = Character(record) Character.cache[name] = c return c
def get_by_id(id, force=False): if not force: Character.check_cache() if not force and id in Character.cache: return Character.cache[id] else: record = CharacterTable().get_by_id(id) if record: c = Character(record) Character.cache[id] = c return c
def modify(request): def set(data, name, value): i = name.find('.') if name in data or i < 0: data[name] = value else: dn = name[0:i] if dn not in data: data[dn] = {} set(data[dn], name[i+1:], value) if 'json' in request.POST: CharacterTable().set_json(request.POST['id'], request.POST['json']) else: j = CharacterTable().get_by_id(request.POST['id'])[6] data = json.loads(j) for name, value in request.POST.items(): if "id" != name: set(data, name, value) j = json.dumps(data, ensure_ascii=False, indent=2) CharacterTable().set_json(request.POST['id'], j) return pcresponse(Character.get_by_id(request.POST['id'], force=True))
def get_character(request): pc = None if 'id' in request.GET: pc = Character.get_by_id(request.GET['id'], force=True) elif 'ch' in request.GET: pc = Character.get_by_name(request.GET['ch'], force=True) if pc: return pcresponse(pc) names = {} for ch in CharacterTable().list(): names[ch[4]] = ch[0] return JsonResponse(names, safe=False, json_dumps_params={'ensure_ascii': False})
def list_by_name(name=None): for row in CharacterTable().list(): if (not name) or (name == '*') or (name.lower() in row[4].lower()): yield Character(row)
def newchar(request): CharacterTable().add(request.POST['json']) data = json.loads(request.POST['json']) return pcresponse(Character.get_by_name(data['name']))
def pcs(request): result = [] records = CharacterTable().get_pcs() for r in records: result.append(Character(r).get_data()) return JsonResponse(result, safe=False, json_dumps_params={'ensure_ascii': False})