from django.core import serializers from models import Equation, Variable, Symbol from django.http import HttpResponse def all_variables(request): data = serializers.serialize("json", Variable.objects.all()) return HttpResponse(data, mimetype="text/javascript") def all_symbols(request): data = serializers.serialize("json", Symbol.objects.all()) return HttpResponse(data, mimetype="text/javascript") def equs_by_vars(request, whichvars): vars = whichvars.split(',') if len(vars) < 1 or whichvars == '': return HttpResponse('[]', mimetype="text/javascript") if vars[-1] == '/' or vars[-1] == '': vars = vars[:-1] returnables = Equation.objects.filter(variables=vars[0]) if len(vars) > 1: for r in returnables: for i in vars[1:]: if len(r.variables.filter(id=i)) == 0: returnables = returnables.exclude(id=r.id) if len(returnables) < 1: break data = serializers.serialize("json", returnables) return HttpResponse(data, mimetype="text/javascript") def vars_by_symbs(request, which_symbols): vars = which_symbols.split(',') if len(vars) < 1 or which_symbols == '': return HttpResponse('[]', mimetype="text/javascript") if vars[-1] == '/' or vars[-1] == '': vars = vars[:-1] #vkeys = list() returnables = list() for v in vars: returnables.append([r for r in Symbol.get_by_id(int(v)).variable_set.run()]) print returnables #vkeys.append(str(Symbol.get_by_id(int(v)).key())) #returnables = Variable.objects.all().filter('symbol IN ', vkeys).run() data = [serializers.serialize("json", r) for r in returnables] return HttpResponse(data, mimetype="text/javascript") #return HttpResponse(returnables, mimetype="text/javascript") def import_symbols(filename): infile = open(filename) stuff = infile.readlines() infile.close() l = eval(stuff[0]) for symb in l: vals = symb['fields'] newsymb = Symbol(latex=vals['latex'], unicode=vals['unicode'], name=vals['name']) newsymb.save() def import_equations(filename): infile = open(filename) stuff = infile.readlines() infile.close() l = eval(stuff[0]) for symb in l: vals = symb['fields'] newsymb = Symbol(latex=vals['latex'], unicode=vals['unicode'], name=vals['name']) newsymb.save() def import_variables(filename): infile = open(filename) stuff = infile.readlines() infile.close() l = eval(stuff[0], {'true':True, 'false':False}) for vari in l: vals = vari['fields'] newvar = Variable(latex=vals['latex'], unicode=vals['unicode'], name=vals['name'], description=vals['description'], reference=vals['reference']) newvar.save() def match_var_symb(): vs = Variable.objects.all().run() for v in vs: try: vname = v.name.split("Generic ")[1] s = Symbol.all().filter('name =', vname).run().next() v.symbol = s v.save() except: print "Error assigning symbol to value: %s " % v