Пример #1
0
def make_expression(filter_dict , name):
    if filter_dict["filter_choice"] != "new_form" and filter_dict["filter_choice"] != name:
        s = dbdb.make_dbdb_session()
        f = s.query(dbdb.Filter).filter_by(name=filter_dict["filter_choice"]).scalar()
        s.close()
        return f.expression.unique_params()
    filter_sel = filter_dict["filter_selector"]
    if filter_sel == "device_type":
        d_t = filter_dict["device_type"]
        if filter_dict.get("device_contains"):
            ret = device.generic_name.contains(d_t)
        else:
            ret = device.generic_name==d_t
    if filter_sel == "date_report":
        date_to = filter_dict["date_to"]
        date_from = filter_dict["date_from"]
        ret = master_record.date_report.between(date_from, date_to)
    if filter_sel == "date_event":
        date_to = filter_dict["date_to"]
        date_from = filter_dict["date_from"]
        ret = master_record.date_event.between(date_from, date_to)
    if filter_sel == "manufacturer":
        man_name = filter_dict["manufacturer_name"]
        ret = device.manufacturer_name.contains(man_name)
    if filter_sel == "event_type":
        e_t = filter_dict["event_type"]
        ret = master_record.event_type==e_t
    if filter_dict["not_op"]:
        ret = not_(ret)
    return ret
Пример #2
0
def save_filter(request, filter_name=None):
    if request.method == "POST":
        OutFormSet = formset_factory(OutForm)
        outformset = OutFormSet(request.POST)
        filterform = FilterForm(request.POST)

        if not outformset.is_valid() or not filterform.is_valid():
            return HttpResponse("invalid form")
        filter_name=filterform.cleaned_data["name"]
        form_li = [i.cleaned_data for i in outformset.forms]
        if len(form_li) > 0:
            form_li[-1]["logical_operation"] = "none"
        dname = {"name" : filter_name}
        if filterform.cleaned_data.get("comment") is "":
            dname["comment"] = filterform.cleaned_data["comment"]
        _filter = make_filter_with_forms(form_li, dname)

        s = make_dbdb_session()
        ret = s.query(Filter).filter_by(name=filter_name).scalar()
        if ret is not None:
            s.delete(ret)
            s.commit()
        s.add(_filter)
        s.commit()
        s.close()

        return HttpResponse("%(name)s has been saved<br>======it looks like======<br> %(expr)s" % {'name' : filter_name, 'expr': _filter.expression})
Пример #3
0
def delete_filter(request, filter_name):
    s = make_dbdb_session()
    ret = s.query(Filter).filter_by(name=filter_name).scalar()
    s.add(ret)
    s.delete(ret)
    s.commit()
    s.close()
    return HttpResponse("%(name)s was deleted" % {'name' : filter_name})
Пример #4
0
def present_dbs(request):
    session = make_dbdb_session()

    topdb =  session.query(Db).filter_by(name="orm_fun").first()

    def display_filters(topdb):
        ret = escape(topdb.name) + "<ul>"
        if topdb.parent_relations:
            ret += "<div class=subf>Filters:"
        for i in topdb.parent_relations:
            ret += "<li class=filter >" + escape(i._filter.name)
            ret += "<a href=/filter/" + escape(i._filter.name) + ">link</a>"
            ret += "<div style=\"margin-left: 10px; display: inline;\">" + "    " + escape(i.child_db_name) + "</div>"
            ret += "</li>"
        ret += "</div>"
        return ret + "</ul>"

    def recurse_dbs(topdb, li):
        child_db_li = topdb.children
        li.append(display_filters(topdb))
        topdb.__setattr__("visited", True)
        subli = []
        for i in child_db_li:
            try:
                i.__getattribute__("visited")
                return "bottom"
            except (AttributeError):
                recurse_dbs(i, subli)
        if subli != []:
            li.append(subli)
        return li

    li = []
    db_tree = recurse_dbs(topdb, li)
    alldbs = session.query(Db).filter(Db.name != "orm_fun").all()
    for db in alldbs:
        if len(db.parents) == 0:
            li = []
            li = recurse_dbs(db, li)
            if len(li) < 2:
                db_tree.append(*li)
            else: 
                db_tree.append(li[0])
                db_tree.append(li[1])

    f_li = session.query(Filter).outerjoin("db_relation").filter(Db_relation.filter_name == None).all()
    filters = f_li
    session.close()
    return render_to_response("db_structure.html",
            {"db_tree" : db_tree, "filters" : filters},
            context_instance=RequestContext(request),
            )
Пример #5
0
def simple_form(request):
    OFset = formset_factory(OutForm,)
    ofset = OFset()
    form = OutForm()
    db_form  = DBForm()
    if request.method == "POST":
        return HttpResponse(o)


    s = make_dbdb_session()
    ret_prox = s.query(Db).all()
    dbs = [(i.name, i.name) for i in ret_prox]
    db_form.fields['input_db'].choices = dbs
    s.close()
    return render_to_response('simple_f.html',
            {'formset': ofset, "db_form" : db_form},
            context_instance=RequestContext(request))
Пример #6
0
 def __init__(self, *args, **kwargs):
     super(forms.Form, self).__init__(*args, **kwargs)
     self.fields['date_from'].widget.attrs['class'] = "date_report date_event"
     self.fields['date_to'].widget.attrs['class'] = "date_report date_event"
     self.fields['device_type'].widget.attrs['class'] = "device_type"
     self.fields['device_contains'].widget.attrs['class'] = "device_type"
     self.fields['manufacturer_name'].widget.attrs['class'] = "manufacturer"
     self.fields['event_type'].widget.attrs['class'] = "event_type"
     self.fields['logical_operation'].widget.attrs['class'] = "notfilter"
     self.fields['not_op'].widget.attrs['class'] = "notfilter"
     self.fields['filter_selector'].widget.attrs['class'] = "filter_selector"
     s = make_dbdb_session()
     f_li =  [(i.name,i.name) for i in s.query(Filter).all()]
     f_li.append(("new_form", "New generic form"))
     f_li = f_li[::-1]
     self.fields["filter_choice"].choices = f_li
     self.fields['filter_choice'].widget.attrs['class'] = "filter_choice"
     s.close()
Пример #7
0
def show_filter(request, filter_name=None):
    if request.method == "GET":
        s = make_dbdb_session()
        fil_to_show = s.query(Filter).filter_by(name=filter_name).scalar()
        _dict = {}
        if fil_to_show is None:
            fil_to_show = Filter(name=filter_name)
            _dict["msg"] = "Save this filter to persist it"
        if len(fil_to_show.forms) == 0:
            OFormSet = formset_factory(OutForm, extra=1)
            _dict["msg"] = "This filter was empty fill out the form to make an expression"
        else:
            OFormSet = formset_factory(OutForm, extra=0)
        formset = OFormSet(initial=[ i.value_dict for i in fil_to_show.forms])
        ff = FilterForm(initial={"name": filter_name, "comment":fil_to_show.comment})
        _dict["formset"] = formset
        _dict["ff"] = ff
        s.close()
        return render_to_response("just_filter.html",
                _dict,
                context_instance=RequestContext(request),
                )
Пример #8
0
def show_expression(request, filter_name):
    s = make_dbdb_session()
    ret = s.query(Filter).filter_by(name=filter_name).first()
    expr = ret.expression
    s.close()
    return HttpResponse("%(name)s  expression looks like this<br> %(expression)s" % {'name' : filter_name, 'expression' : expr})
Пример #9
0
def out_form(request):
    if request.method== "POST":
        OutFormSet = formset_factory(OutForm)
        outformset = OutFormSet(request.POST)
        if not outformset.is_valid():
            HttpResponse("formset is not valid")
        db_form = DBForm(request.POST)
        s = make_dbdb_session()
        ret_prox = s.query(Db).all()
        dbs = [(i.name, i.name) for i in ret_prox]
        s.close()
        del ret_prox
        db_form.fields['input_db'].choices = dbs
        db_form.full_clean()
        input_db_name = db_form.cleaned_data["input_db"]
        if not verify_db_name(input_db_name):
            return HttpResponse("need a real dbname")
        session = make_input_db_session(input_db_name, echo=ECHO)

        start = time.time()
        base_q = session.query(device).join("master_record")

        filter_dict_li = [i.cleaned_data for i in outformset.forms]

        filter_to_commit = make_filter_with_forms(filter_dict_li,
                {"name":db_form.cleaned_data["filter_name"]})

        base_q = base_q.filter(filter_to_commit.expression)
        base_q = base_q.\
                        options(joinedload(device.problems), joinedload(device.master_record))
        ret_o = base_q.all()

        li_to_commit = ret_o
        for i in ret_o:

            def span_left_from_dev(j):
                if j.text_records:
                    [make_transient(i) for i in j.text_records]
                if j.patients:
                    [make_transient(i) for i in j.patients]
                make_transient(j)

            def span_right_from_dev(j):
                if j.problem_code: make_transient(j.problem_code)
                make_transient(j)

            [span_right_from_dev(j) for j in i.problems]
            span_left_from_dev(i.master_record)
            make_transient(i)
        session.close()
        db_name = db_form.cleaned_data["output_db"]
        engine1 = create_engine("mysql://root@localhost:3306/mysql")
        if db_name in db_deny_set:
            return HttpResponse("You can't play with that db")

        if " " in db_name:
            return HttpResponse("no spaces here")

        dbdb_session = make_dbdb_session()
        ret_prox = dbdb_session.query(Db).all()
        dbli = [i.name for i in ret_prox]
        if db_name in dbli:
            if db_form.cleaned_data["drop_output_selector"]:
                engine1.execute("drop database " + db_name)
                engine1.execute("create database " + db_name)

            i = dbli.index(db_name)
            db_entry = ret_prox[i]
            db_entry.date_created = datetime.now()
            relation = Db_relation(child_db_name=db_name,parent_db_name=input_db_name)
        else:
            db_entry = Db(name=db_name, date_created=datetime.now())
            relation = Db_relation(parent_db_name=input_db_name, child_db_name=db_name)
            engine1.execute("create database " + db_name)
        dbdb_session.add(filter_to_commit)
        dbdb_session.commit()
        relation._filter = filter_to_commit
        db_entry.child_relations.append(relation)
        dbdb_session.add(db_entry)
        dbdb_session.commit()
        dbdb_session.close()
        del engine1

        engine1b = create_engine('mysql://root@localhost:3306/' + db_name)
        Base.metadata.create_all(bind=engine1b)
        del engine1b

        engine2 = create_engine('mysql://root@localhost:3306/' + db_name, echo=ECHO)
        nDBSesh = sessionmaker(bind=engine2)()
        #adds on duplicate key... to the mass inserts for the newly created db
        @compiles(Insert)
        def add_string(insert, compiler, **kw):
            s = compiler.visit_insert(insert, **kw)
            MDR_tables = [
                    "master_table",
                    "text_table",
                    "patient_table",
                    "device_problem_table",
                    "device_table",
                    ]
            rems = ""
            if insert.table.name == "problem_code_table":
                rems = "device_problem_code = device_problem_code"
            elif insert.table.name in MDR_tables:
                rems = "MDR_report_key = MDR_report_key"
            return s + " ON DUPLICATE KEY UPDATE " + rems
        nDBSesh.add_all(li_to_commit)
        nDBSesh.commit()
        nDBSesh.execute("CREATE INDEX date_index ON master_table (date_report)")
        nDBSesh.execute("CREATE INDEX mdr_key_index ON text_table (MDR_report_key)")
        nDBSesh.close()
        del engine2
        #redefining add_string prevents insert dying on other things
        @compiles(Insert)
        def add_string(insert, compiler, **kw):
            s = compiler.visit_insert(insert, **kw)
            return s

        stop = time.time()
        dif = stop - start
        return HttpResponse("inserted this many records into new db: " + str(len(ret_o)) + "<br>"  + str(dif)+ "<br><br>" +
                "====The expression made looked like this==== <br>" + str(filter_to_commit.expression) + "")

    else:
        return HttpResponse("You gotta post")
Пример #10
0
    def __init__(self, *args, **kwargs):
        super(forms.Form, self).__init__(*args, **kwargs)
        self.fields['patient_field_selector'].widget.attrs['class'] = tf
        self.fields['master_record_field_selector'].widget.attrs['class'] = tf
        self.fields['device_field_selector'].widget.attrs['class'] = tf
        self.fields['problem_code_field_selector'].widget.attrs['class'] = tf
        self.fields['FOI_text_field_selector'].widget.attrs['class'] = tf







s = make_dbdb_session()
ret_prox = s.query(Db).all()
dbs = [(i.name, i.name) for i in ret_prox]
s.close()

class RawForm(forms.Form):
    db_to_select_from = forms.ChoiceField(choices=dbs, required=True)
    select_body = forms.CharField(required=True)

    def __init__(self, *args, **kwargs):
        super(RawForm, self).__init__(*args, **kwargs)
        self.fields['select_body'].widget = forms.Textarea()