Exemplo n.º 1
0
def sub_tables(request):
    tbl_name = request.GET.get('tbl_name', None)
    ucan_id = request.GET.get('ucan_id', None)
    network_station_ids_list = request.GET.getlist('network_station_id', [])
    network_station_ids_list = [str(n_id) for n_id in network_station_ids_list]
    context = {
        'title': 'Table ADD Tool',
        'tbl_name': tbl_name,
        'network_station_ids_list':network_station_ids_list,
        'ucan_id': ucan_id
    }
    obj = secondary_tables[tbl_name]
    table_dicts = {}
    max_instances = 0
    for idx, network_station_id in enumerate(network_station_ids_list):
        table_dicts[network_station_id] = []
        instances = obj.objects.filter(network_station_id=network_station_id)
        if len(instances) > max_instances:max_instances = len(instances)
        for inst_idx, inst in enumerate(instances):
            inst_list = convert_query_set(inst,'python_list')
            table_dicts[network_station_id].append(inst_list)
    #Format for html loop
    station = convert_query_set(models.Station.objects.filter(ucan_station_id=ucan_id), 'python_dict')
    init = {
        'ucan_station_id':ucan_id,
        'begin_date':WRCCUtils.convert_db_dates(station['begin_date']),
        'end_date':WRCCUtils.convert_db_dates(station['end_date']),
    }
    form_class = getattr(mforms, tbl_name)
    form = form_class()
    if max_instances == 0:
        results = [[[] for idx in range(len(network_station_ids_list)+1)] for inst in range(1)]
        for idx, network_station_id in enumerate(network_station_ids_list):
            init['network_station_id'] = network_station_id
            for key, val in form.__dict__.iteritems():
                if key[0]!= '_':
                    if key in init.keys():
                        results[0][idx].append([key, init[key]])
                    else:
                        results[0][idx].append([key, val])
        #results[0][-1] = results[0][-2]
        for idx in range(len(results[0]) - 1):
            results[0][idx] = []
    else:
        results = [[[] for idx in range(len(network_station_ids_list)+1)] for inst in range(max_instances)]
        for inst in range(max_instances):
            for idx, network_station_id in enumerate(network_station_ids_list):
                if len(table_dicts[network_station_id]) > inst:
                    results[inst][idx]=table_dicts[network_station_id][inst]
                else:
                    continue
        #add forms at end of each row
        for idx, network_station_id in enumerate(network_station_ids_list):
            if not table_dicts[network_station_id]:
                init['network_station_id'] = network_station_id
                for key, val in form.__dict__.iteritems():
                    if key[0]!= '_':
                        if key in init.keys():
                            results[idx][-1].append([key, init[key]])
                        else:
                            results[idx][-1].append([key, val])
            else:
                results[idx][-1]=results[idx][idx]

    #Write to metadata load file
    if 'form_add' in request.POST or 'form_edit' in request.POST:
        form_idx = int(request.POST['form_id']) - 1
        for key in request.POST:
            for idx,key_val in enumerate(results[form_idx][-1]):
                if str(key) == key_val[0]:
                    results[form_idx][-1][idx][1] = str(request.POST[key])
        meta_str = ''
        with open(load_tables_dir + load_tables[tbl_name],'a+') as f:
            for idx,key in enumerate(key_list[tbl_name]):
                if idx != len(key_list[tbl_name]) - 1:
                    end_char = '|'
                else:
                    end_char = '\n'
                try:
                    meta_str+=str(request.POST[key]) + end_char
                except:
                    #WTF?? Some db Variable tables have 'network' key but some have 'network_id' key
                    #instead
                    if key == 'network':
                        meta_str+=str(request.POST['network_id']) + end_char

            if 'form_edit' in request.POST:
                pass
            else:
                f.write(meta_str)
        #Double check
        #os.chmod(load_tables_dir + load_tables[tbl_name], 0777)
        with open(load_tables_dir + load_tables[tbl_name], 'r') as f:
            if f.readlines()[-1] == meta_str:
                context['merge_successful'] = True
                context['form_id'] = int(request.POST['form_id'])
    context['results'] = results
    return render_to_response('wrcc_meta/sub_tables.html', context, context_instance=RequestContext(request))
Exemplo n.º 2
0
def station_tables_merge(request):
    tbl_name = request.GET.get('tbl_name', None)
    ucan_id_list = request.GET.getlist('ucan_id', [])
    ucan_id_list = [str(uid) for uid in ucan_id_list]
    tbl_done_list = request.GET.getlist('tbl_done', [])
    context = {
        'title': tbl_name + ' Table Merge Tool',
        'tbl_done_list':tbl_done_list
    }
    tbl_done = False
    if tbl_name in tbl_done_list:tbl_done=True;context['tbl_done']=True
    if not tbl_name or not ucan_id_list:
        return render_to_response('wrcc_meta/station_tables_merge.html', context, context_instance=RequestContext(request))
    context['ucan_id_list'] = ucan_id_list
    context['tbl_name'] = tbl_name
    table_dicts = {}
    #Find table instances for each ucan id
    ucan_station_id_acis = None
    ucan_station_id_form = None
    wrcc_id = None
    network_station_ids_dict = {}
    for idx, ucan_id in enumerate(ucan_id_list):
        if int(ucan_id) < 1000000:
            ucan_station_acis = ucan_id
        else:
            wrcc_id = ucan_id
        table_dicts[ucan_id] = []
        obj = primary_tables[tbl_name]
        if tbl_name == 'StationNetwork':
            network_station_ids_dict[ucan_id] = []
        instances = obj.objects.filter(ucan_station_id=ucan_id)
        for i, instance in enumerate(instances):
            inst_list = convert_query_set(instance,'python_list')
            table_dicts[ucan_id].append(inst_list)
            #Get network_station_id for Variable, StationDigital tables
            #if we are looking at the StationNetwork table
            if tbl_name == 'StationNetwork':
                inst_dict = convert_query_set(instance,'python_dict')
                network_station_ids_dict[ucan_id].append(str(inst_dict['network_station_id']))
    context['network_station_ids_dict'] = network_station_ids_dict

    #query station_table entries in db for wrcc id entry
    if wrcc_id:
        station = convert_query_set(models.Station.objects.filter(ucan_station_id=wrcc_id), 'python_dict')
        if ucan_station_id_acis:
            ucan_station_id_form = ucan_station_id_acis
        else:
            ucan_station_id_form = ucan_id_list[0]
    else:
        if ucan_station_id_acis:
            station = convert_query_set(models.Station.objects.filter(ucan_station_id=ucan_station_id_form), 'python_dict')
            ucan_station_id_form = ucan_station_id_acis
        else:
            station = convert_query_set(models.Station.objects.filter(ucan_station_id=ucan_id_list[0]), 'python_dict')
            ucan_station_id_form = ucan_id_list[0]

    #Reorder results for easy html formatting
    max_instances = max([len(table_dicts[uid]) for uid in ucan_id_list])
    if  max_instances == 0:
        #set up blank form for table
        #get station table for start, end dates and data flags
        init = {
            'ucan_station_id':ucan_station_id_form,
            'begin_date':WRCCUtils.convert_db_dates(station['begin_date']),
            'end_date':WRCCUtils.convert_db_dates(station['end_date']),
            'begin_date_flag':station['begin_date_flag'],
            'end_date_flag':station['end_date_flag'],
            'history_flag':station['history_flag'],
            'src_quality_code':station['src_quality_code'],
            'updated_by': 'WRCCsync',
            'last_updated':today_yr + '-' + today_month + '-' + today_day
            }
        form_class = getattr(mforms, tbl_name + 'Form')
        form = form_class(initial=init)
        for field in form.fields:
            if form[field].value():
                table_dicts[ucan_station_id_form].append([str(field), str(form[field].value())])
            else:
                table_dicts[ucan_station_id_form].append([str(field),''])
        results=[[table_dicts[ucan_station_id_form]]]
    else:
        #Format for html display
        results = [[[] for idx in range(len(ucan_id_list)+1)] for inst in range(max_instances)]
        for inst in range(max_instances):
            for idx, ucan_id in enumerate(ucan_id_list):
                if len(table_dicts[ucan_id]) > inst:
                    results[inst][idx]=table_dicts[ucan_id][inst]
                    if ucan_id >1000000:
                        results[inst][-1] = []
                        for idx, key_val in enumerate(table_dicts[ucan_id][inst]):
                            results[inst][-1].append([key_val[0], key_val[1]])
            if not results[inst][-1]:
                for idx, key_val in enumerate(table_dicts[ucan_id_list[-1]][inst]):
                    results[inst][-1].append([key_val[0], key_val[1]])
            #Overwrite editable form with wrcc values if they exist
            #and replace updated_by, last _updated and ucan_id
            context['x'] =  WRCCUtils.convert_db_dates(station['begin_date'])
            if tbl_done:
                for idx, key_val in enumerate(results[inst][-1]):
                    results[inst][-1][idx][1] = ''
            else:
                for idx, key_val in enumerate(results[inst][-1]):
                    if str(key_val[0]) == 'ucan_station_id': results[inst][-1][idx][1]= ucan_station_id_form
                    if str(key_val[0]) == 'begin_date_flag': results[inst][-1][idx][1]= station['begin_date_flag']
                    if str(key_val[0]) == 'begin_date': results[inst][-1][idx][1]= WRCCUtils.convert_db_dates(station['begin_date'])
                    if str(key_val[0]) == 'end_date_flag': results[inst][-1][idx][1]= station['end_date_flag']
                    if str(key_val[0]) == 'end_date': results[inst][-1][idx][1]= WRCCUtils.convert_db_dates(station['end_date'])
                    if str(key_val[0]) == 'history_flag': results[inst][-1][idx][1]= station['history_flag']
                    if str(key_val[0]) == 'src_quality_code': results[inst][-1][idx][1]= station['src_quality_code']
                    if str(key_val[0]) == 'last_updated':results[inst][-1][idx][1] = today_yr + '-' + today_month + '-' + today_day
                    if str(key_val[0]) == 'updated_by':results[inst][-1][idx][1]= 'WRCCSync'
    context['results'] = results

    #Write merge information to metadata.load file
    if 'form_merge' in request.POST:
        form_idx = int(request.POST['form_id']) - 1
        #Override merge table with new entries
        for key in request.POST:
            for idx,key_val in enumerate(results[form_idx][-1]):
                if str(key) == key_val[0]:
                    results[form_idx][-1][idx][1] = str(request.POST[key])
        meta_str = ''
        with open(load_tables_dir + load_tables[tbl_name],'a+') as f:
            for idx,key in enumerate(key_list[tbl_name]):
                if idx != len(key_list[tbl_name]) - 1:
                    meta_str+=str(request.POST[key]) + '|'
                else:
                    meta_str+=str(request.POST[key]) + '\n'
            f.write(meta_str)
        #Double check
        #os.chmod(load_tables_dir + load_tables[tbl_name], 0777)
        with open(load_tables_dir + load_tables[tbl_name], 'r') as f:
            if f.readlines()[-1] == meta_str:
                context['merge_successful'] = True
                context['form_id'] = int(request.POST['form_id'])
    return render_to_response('wrcc_meta/station_tables_merge.html', context, context_instance=RequestContext(request))