def post(self,request): request.POST=request.POST.copy() # print(request.POST) data=dict() eva_id=int(request.POST['pk']) temp_eva_ref=db.collection('evaluate').where('eva_id','==',eva_id) temp_eva_data=[i.to_dict() for i in temp_eva_ref.stream()][0] # member_ref=db.collection('member').where('member_id','in',[int(i) for i in temp_eva_data['member_lineitem'].keys()]) # member_data=[(i.to_dict())['member_id'] for i in member_ref.stream()] for k,v in temp_eva_data['member_lineitem'].items(): temp={} member_ref=db.collection('member').where('member_id','==',int(k)) member_data=[i.to_dict() for i in member_ref.stream()][0] member_doc_id=[i.id for i in member_ref.stream()][0] if v==1: temp['sum_rehearsal']=member_data['sum_rehearsal']-1 temp['count_rehearsal']=member_data['count_rehearsal']-1 db.collection('member').document(member_doc_id).update(temp) # for doc in member_ref.stream(): # doc.update(temp) # member_ref.document(doc.id).update(temp) for doc in temp_eva_ref.stream(): doc.reference.delete() # print(temp_eva_data) # print(member_data) return JsonResponse(data)
def get(self, request, pk): data = dict() event = db.collection('event').where('event_id', '==', int(pk)) event = [i.to_dict() for i in event.stream()][0] temp = dict() for i in ['date_time', 'location', 'event_name', 'event_id']: temp[i] = event[i] # data['header']=temp # temp_type_dance=set() # temp_member=[] # temp_song=set() # for k,v in event['line_item'].items(): # temp_type_dance.add(v['type_dance']) # temp_member.extend(v['member']) # temp_song.add(v['song_id']) # temp_type_dance=list(temp_type_dance) # temp_member=list(set(temp_member)) # temp_song=list(temp_song) # song_ref=db.collection('song').where('song_id','in',temp_song) # member_ref=db.collection('member').where('member_id','in',temp_member) # type_ref=db.collection('type_dance').where('type_id','in',temp_type_dance) # for k,v in event['line_item'].items(): # event['line_item'][k]['song']= # temp_song=[[i,[j.to_dict()['song_name'] for j in song_ref.stream() if j.to_dict()['song_id']==i][0]] for i in temp_song] # # data['lineitem']=event['line_item'] # data['lineitem']=temp_song data = event return JsonResponse(data)
def create_new_eva(): eva_main_df = pd.read_csv("Data/evaluate_main.csv") eva_member_df = pd.read_csv("Data/evaluate_member.csv") eva_score_df = pd.read_csv("Data/evaluate_score.csv") ref = db.collection("evaluate") for index, row in eva_main_df.iterrows(): ans = row.to_dict() temp_member = eva_member_df[eva_member_df['eva_id'] == row['eva_id']] member_lineitem = {} for index2, row2 in temp_member.iterrows(): # temp_lineitem=dict() # temp_lineitem['member_id']=row2['member_id'] # temp_lineitem['has_come']=row2['has_come'] member_lineitem[str(int(row2['member_id']))] = int( row2['has_come']) ans['member_lineitem'] = member_lineitem score_lineitem = {} temp_score = eva_score_df[eva_score_df['eva_id'] == row['eva_id']] for index2, row2 in temp_score.iterrows(): score_lineitem[str(int(row2['criteria_id']))] = int( row2['eva_score']) ans['score_lineitem'] = score_lineitem # print(sum(ans['score_lineitem'].values())/len(ans['score_lineitem'].values())) if len(ans['score_lineitem'].values()) == 0: ans['mean_score'] = None else: ans['mean_score'] = sum(ans['score_lineitem'].values()) / len( ans['score_lineitem'].values()) print(ans) ref.document().set(ans)
def get(self, request): data = dict() td_ref = db.collection('type_dance') td_data = [i.to_dict() for i in td_ref.stream()] # print(td_data) data['type_dance'] = td_data return JsonResponse(data)
def get(self, request): data = dict() member_ref = db.collection('member') num_member = len([i for i in member_ref.stream()]) event_ref = db.collection('event') int_sum = 0 for doc in event_ref.stream(): temp = list() temp_dict = doc.to_dict() print(temp_dict) for k, v in temp_dict['line_item'].items(): temp.extend(v['member']) temp = list(set(temp)) int_sum += len(temp) print(num_member, int_sum) data['mean'] = int_sum / num_member return JsonResponse(data)
def get(self,request): cri_ref=db.collection('criteria') data=dict() temp=[] for doc in cri_ref.stream(): temp.append(doc.to_dict()) data['criteria']=temp return JsonResponse(data)
def get(self, request): data = dict() event_list = db.collection('event').order_by( 'date_time', direction=firestore.Query.DESCENDING) temp = [] for doc in event_list.stream(): temp.append(doc.to_dict()) data['event_list'] = temp return JsonResponse(data)
def get(self,request,pk1,pk2): data=dict() # Get heaer Data event_ref=db.collection('event').document(pk1) event_data=event_ref.get().to_dict() data['event_name']=event_data['event_name'] event_data['line_item']=event_data['line_item'][pk2] song_ref=db.collection('song').where('song_id','==',event_data['line_item']['song_id']) for doc in song_ref.stream(): data['song_name']=(doc.to_dict())['song_name'] #Score From Criteria and Score Overall eva_ref=db.collection('evaluate').where('event_id','==',int(pk1)).where('team_id','==',int(pk2)) criteria_ref=db.collection('criteria') data['list_evaluation']=[] temp_list_score=[] for doc in eva_ref.stream(): temp_dict=dict() temp_doc=doc.to_dict() temp_dict['eva_id']=temp_doc['eva_id'] temp_dict['eva_date']=temp_doc['eva_date'] temp_dict['mean_score']=temp_doc['mean_score'] temp_dict['criteria_score']=temp_doc['score_lineitem'] temp_list_score.append(temp_doc['member_lineitem']) for cri_doc in criteria_ref.stream(): temp=cri_doc.to_dict() # print(temp['criteria_id'],temp['criteria_name']) # print(temp_dict['criteria_score'][str(temp['criteria_id'])]) # print(temp_dict['criteria_score']) try: temp_dict['criteria_score'][str(temp['criteria_id'])]=[temp_dict['criteria_score'][str(temp['criteria_id'])],temp['criteria_name']] except: print() data['list_evaluation'].append(temp_dict) data['list_evaluation'].sort(key=lambda i:i['eva_date']) #member Part member_result = dict(functools.reduce(operator.add, map(collections.Counter, temp_list_score))) member_ref=db.collection('member').where('member_id','in',event_data['line_item']['member']) for doc in member_ref.stream(): temp_doc=doc.to_dict() member_result[str(temp_doc['member_id'])]=[member_result[str(temp_doc['member_id'])],temp_doc['nickname']] data['list_member']=member_result # print(data) return JsonResponse(data)
def post(self,request): print("Hello") request.POST=request.POST.copy() eva_id=request.POST['pk'] docs = db.collection('evaluate').where('eva_id','==',int(eva_id)).stream() data=[i.to_dict() for i in docs][0] # data=dict() print(data) print("***") return JsonResponse(data)
def get(self, request, pk): member_ref = db.collection('member').where('member_id', '==', int(pk)) data = dict() for doc in member_ref.stream(): data['member_detail'] = doc.to_dict() event_ref = db.collection('event') list_event = [] for doc in event_ref.stream(): temp = doc.to_dict() temp2 = temp['line_item'] temp2 = list(temp2.values()) temp2 = any(int(pk) in i['member'] for i in temp2) if temp2: temp_dict = {} temp_dict['event_name'] = temp['event_name'] temp_dict['date_time'] = temp['date_time'] list_event.append(temp_dict) data['list_event'] = list_event # print(data) return JsonResponse(data)
def get(self, request): member_ref = db.collection('member').order_by('nickname') docs = member_ref.stream() member = [] data = dict() for doc in docs: temp = doc.to_dict() temp['name'] = temp['firstname'] + " " + temp['lastname'] del temp['firstname'] member.append(temp) print("{} member loaded".format(len(member))) data['member'] = member return JsonResponse(data)
def get(self, request): song_ref = db.collection('song').order_by('song_name') docs = song_ref.stream() song = [] data = dict() for doc in docs: temp = doc.to_dict() if temp['song_ytlink'] != None: temp['song_ytlink'] = temp['song_ytlink'].split('/')[-1] temp['song_ytlink'] = temp['song_ytlink'][8:] # print(temp) song.append(temp) print("{} song loaded".format(len(song))) data['songs'] = song return JsonResponse(data)
def create_new_event(): event_df = pd.read_csv("Data/event.csv") event_line_item_df = pd.read_csv("Data/event_line_item.csv") ref = db.collection('event') for index, row in event_df.iterrows(): ans = row.to_dict() temp = event_line_item_df[event_line_item_df['event_id'] == row['event_id']] team_lineitem = {} for i in temp['team_id'].unique(): # print(int(i),type(int(i))) temp2 = temp[temp['team_id'] == i] temp_lineitem = dict() temp_lineitem['type_dance'] = int(temp2['type_id'].unique()[0]) temp_lineitem['song_id'] = int(temp2['song_id'].unique()[0]) temp_lineitem['member'] = [ int(i) for i in temp2['member_id'].unique() ] team_lineitem[str(int(i))] = temp_lineitem ans['line_item'] = team_lineitem print(ans) ref.document(str(row['event_id'])).set(ans)
def init(tableName): print("Start initial", tableName) start = time.time() df = pd.read_csv("Data/" + tableName + ".csv") df = df.where((pd.notnull(df)), None) inputs = df.to_dict('record') print(tableName, "dict created", time.time() - start, "seconds") batch = db.batch() ref = db.collection(tableName) docs = ref.list_documents() print(tableName, "reference Created") howmany = 0 for doc in docs: # doc.reference.delete() batch.delete(doc) howmany += 1 if howmany == 490: batch.commit() howmany = 0 print(tableName, "old data Deleted", time.time() - start, "seconds") for inputt in inputs: # ref.add(inputt) batch.set(ref.document(), inputt) howmany += 1 if howmany == 490: batch.commit() howmany = 0 batch.commit() print(tableName, "Finished", time.time() - start, "seconds") del ref, docs, inputs, df return True
def post(self,request): data=dict() request.POST=request.POST.copy() data=request.POST del data['csrfmiddlewaretoken'] data['member_lineitem']=eval(data['member_lineitem'])['member_lineitem'] eva_data=[i.to_dict() for i in db.collection('evaluate').where('eva_id','==',int(data['eva_id'])).stream()][0] doc_ans_id=[i.id for i in db.collection('evaluate').where('eva_id','==',int(data['eva_id'])).stream()][0] data['event_id']=int(data['event_id']) data['team_id']=int(data['team_id']) data['eva_id']=int(data['eva_id']) print(eva_data) temp={} for d in data['member_lineitem']: for k,v in d.items(): temp[k]=v member_ref=db.collection('member').where('member_id','==',int(k)) data_member=dict() doc_id="" for doc in member_ref.stream(): data_member=doc.to_dict() doc_id=doc.id if v>eva_data['member_lineitem'][k]: data_member['sum_rehearsal']+=1 elif v<eva_data['member_lineitem'][k]: data_member['sum_rehearsal']-=1 # data_member['count_rehearsal']+=1 db.collection('member').document(doc_id).update(data_member) data['member_lineitem']=temp data['score_lineitem']=eval(data['score_lineitem'])['score_lineitem'] temp={} for d in data['score_lineitem']: for k,v in d.items(): temp[k]=int(v) data['score_lineitem']=temp data['mean_score']=sum(list(data['score_lineitem'].values()))/len(list(data['score_lineitem'].values())) db.collection('evaluate').document(doc_ans_id).update(data) return JsonResponse(data)
def post(self,request): data=dict() request.POST=request.POST.copy() # print(request.POST) query=db.collection('evaluate').order_by('eva_id',direction=firestore.Query.DESCENDING).limit(1) max_num=[i.to_dict() for i in query.stream()][0]['eva_id'] request.POST['eva_id']=(max_num+1) data=request.POST del data['csrfmiddlewaretoken'] data['member_lineitem']=eval(data['member_lineitem'])['member_lineitem'] data['event_id']=int(data['event_id']) data['team_id']=int(data['team_id']) # data['member_lineitem']=[i[0] for i in data['member_lineitem']] temp={} for d in data['member_lineitem']: for k,v in d.items(): temp[k]=v member_ref=db.collection('member').where('member_id','==',int(k)) data_member=dict() doc_id="" for doc in member_ref.stream(): data_member=doc.to_dict() doc_id=doc.id if v==1: data_member['sum_rehearsal']+=1 data_member['count_rehearsal']+=1 db.collection('member').document(doc_id).update(data_member) # print(temp) data['member_lineitem']=temp data['score_lineitem']=eval(data['score_lineitem'])['score_lineitem'] # data['score_lineitem']=[i[0] for i in data['score_lineitem']] temp={} for d in data['score_lineitem']: for k,v in d.items(): temp[k]=int(v) data['score_lineitem']=temp data['mean_score']=sum(list(data['score_lineitem'].values()))/len(list(data['score_lineitem'].values())) # print(data) db.collection('evaluate').add(data) return JsonResponse(data)
from dbHelper import db # Create a callback on_snapshot function to capture changes def on_snapshot(doc_snapshot, changes, read_time): for doc in doc_snapshot: print('Received document snapshot: {}'.format(doc.id, doc.to_dict())) print(changes, read_time) doc_ref = db.collection('song') # Watch the document doc_ref.on_snapshot(on_snapshot)