def data_today(request): if os.environ.get("TEST", None): request.session["userId"] = request.GET["userId"] if not "userId" in request.session: data = {"error": {"title": u"未绑定", "content": u"请先到公众号页面绑定手环"}} return HttpResponse(json.dumps(data), content_type="application/json") try: if not request.GET["userId"] == request.session["userId"]: data = {"error": {"title": u"权限问题", "content": u"您无权访问此页面"}} return HttpResponse(json.dumps(data), content_type="application/json") data = {} date = tools.getDate() data["userId"] = userId = request.session["userId"] data["href"] = "%s/basic/redirect/profile" % wechat_tools.domain user = User.objects.get(openId=userId) dayData = DayData.objects.filter(user=user, date=date) if len(dayData) == 0: dayData = DayData(user=user, date=date) dayData.save() else: dayData = dayData[0] data_list = Data.objects.filter(user=user, date=date) data["steps_now"] = dayData.steps data["steps_plan"] = user.dayPlan if data["steps_now"] < data["steps_plan"]: data["steps_per"] = data["steps_now"] * 100 / data["steps_plan"] else: data["steps_per"] = 100 data["sleep_now_h"] = dayData.sleep / 60 data["sleep_now_m"] = dayData.sleep % 60 data["sleep_plan"] = user.sleepPlan if data["sleep_now_h"] < data["sleep_plan"]: data["sleep_per"] = int( (data["sleep_now_h"] + float(data["sleep_now_m"]) / 60) * 100 / data["sleep_plan"]) else: data["sleep_per"] = 100 data["data_list"] = [] for data_item in data_list: item = {} item["date"] = "%4d-%2d-%2d" % (date / 10000, date % 10000 / 100, date % 100) item["time"] = data_item.startTime.split(" ")[1][:5] type = data_item.type subType = data_item.subType item["content"] = tools.bong_activity[type - 1][subType] data["data_list"].append(item) except: data = {"error": {"title": u"出错啦", "content": u"页面找不到啦!"}} return HttpResponse(json.dumps(data), content_type="application/json")
def data_rank(request): if os.environ.get("TEST", None): request.session["userId"] = request.GET["userId"] if not "userId" in request.session: data = {"error": {"title": u"未绑定", "content": u"请先到公众号绑定手环"}} return HttpResponse(json.dumps(data), content_type="application/json") try: data = {} date = tools.getDate() users = User.objects.all() userId = data["userId"] = request.session["userId"] data["href"] = "%s/basic/redirect/profile" % wechat_tools.domain owner = User.objects.get(openId=userId) data["data_list"] = [] num = 1 for user in users: item = {} item["identity"] = 2 if user.openId == userId: item["identity"] = 0 else: flag = owner.friends.filter(openId=user.openId) if len(flag) > 0: item["identity"] = 1 flag = Good.objects.filter(user=owner, type=0, target=user.id) if len(flag) > 0: item["isGood"] = 1 item["userId"] = user.openId item["num"] = 0 item["username"] = user.name item["image"] = user.image dayData = DayData.objects.filter(user=user, date=date) if len(dayData) == 0: dayData = DayData(user=user, date=date) dayData.save() else: dayData = dayData[0] item["steps"] = dayData.steps item["goods"] = user.goods data["data_list"].append(item) data["data_list"] = sorted(data["data_list"], key=lambda user: user["steps"], reverse=True) ld = len(data["data_list"]) for i in xrange(ld): data["data_list"][i]["num"] = i + 1 except: data = {"error": {"title": u"出错啦", "content": u"页面找不到啦!"}} return HttpResponse(json.dumps(data), content_type="application/json")
def data_rank(request): if os.environ.get("TEST", None): request.session["userId"] = request.GET["userId"] if not "userId" in request.session: data = {"error":{"title":u"未绑定","content":u"请先到公众号绑定手环"}} return HttpResponse(json.dumps(data), content_type="application/json") try: data = {} date = tools.getDate() users = User.objects.all() userId = data["userId"] = request.session["userId"] data["href"] = "%s/basic/redirect/profile" % wechat_tools.domain owner = User.objects.get(openId=userId) data["data_list"] = [] num = 1 for user in users: item = {} item["identity"] = 2 if user.openId == userId: item["identity"] = 0 else: flag = owner.friends.filter(openId=user.openId) if len(flag) > 0: item["identity"] = 1 flag = Good.objects.filter(user=owner,type=0,target=user.id) if len(flag) > 0: item["isGood"] = 1 item["userId"] = user.openId item["num"] = 0 item["username"] = user.name item["image"] = user.image dayData = DayData.objects.filter(user=user,date=date) if len(dayData) == 0: dayData = DayData(user=user,date=date) dayData.save() else: dayData = dayData[0] item["steps"] = dayData.steps item["goods"] = user.goods data["data_list"].append(item) data["data_list"] = sorted(data["data_list"], key=lambda user : user["steps"], reverse=True) ld = len(data["data_list"]) for i in xrange(ld): data["data_list"][i]["num"] = i+1 except: data = {"error":{"title":u"出错啦","content":u"页面找不到啦!"}} return HttpResponse(json.dumps(data), content_type="application/json")
def data_today(request): if os.environ.get("TEST", None): request.session["userId"] = request.GET["userId"] if not "userId" in request.session: data = {"error":{"title":u"未绑定","content":u"请先到公众号页面绑定手环"}} return HttpResponse(json.dumps(data), content_type="application/json") try: if not request.GET["userId"] == request.session["userId"]: data = {"error":{"title":u"权限问题","content":u"您无权访问此页面"}} return HttpResponse(json.dumps(data), content_type="application/json") data = {} date = tools.getDate() data["userId"] = userId = request.session["userId"] data["href"] = "%s/basic/redirect/profile" % wechat_tools.domain user = User.objects.get(openId=userId) dayData = DayData.objects.filter(user=user,date=date) if len(dayData) == 0: dayData = DayData(user=user,date=date) dayData.save() else: dayData = dayData[0] data_list = Data.objects.filter(user=user,date=date) data["steps_now"] = dayData.steps data["steps_plan"] = user.dayPlan if data["steps_now"] < data["steps_plan"]: data["steps_per"] = data["steps_now"] * 100 / data["steps_plan"] else: data["steps_per"] = 100 data["sleep_now_h"] = dayData.sleep / 60 data["sleep_now_m"] = dayData.sleep % 60 data["sleep_plan"] = user.sleepPlan if data["sleep_now_h"] < data["sleep_plan"]: data["sleep_per"] = int((data["sleep_now_h"] + float(data["sleep_now_m"]) / 60) * 100 / data["sleep_plan"]) else: data["sleep_per"] = 100 data["data_list"] = [] for data_item in data_list: item = {} item["date"] = "%4d-%2d-%2d" % (date / 10000, date % 10000 / 100, date % 100) item["time"] = data_item.startTime.split(" ")[1][:5] type = data_item.type subType = data_item.subType item["content"] = tools.bong_activity[type-1][subType] data["data_list"].append(item) except: data = {"error":{"title":u"出错啦","content":u"页面找不到啦!"}} return HttpResponse(json.dumps(data), content_type="application/json")
def get_day_data_objects(filename): from models import engine, db_session, DayData, Country with open(filename, 'r') as readfile: reader = csv.reader(readfile) day_data_list = [] for index, row in enumerate(reader): if index == 0: continue print(f'row: {row}') if index < 5: continue print(f'row[0]: {row[0]}') country = db_session.query(Country).filter_by(name=row[0]).first() date_data = [ int(date_component) for date_component in row[1].split('-') ] try: country_date = DayData( country=country, date=datetime.date(date_data[0], date_data[1], date_data[2]), new_cases=int(row[2]), new_deaths=int(float(row[3])), ) print(f'country: {country_date.new_cases}') day_data_list.append(country_date) except ValueError: country_date = DayData(country=country, date=datetime.date( date_data[0], date_data[1], date_data[2]), new_cases=int(row[2]), new_deaths=0) day_data_list.append(country_date) return day_data_list
def update_from_latest_file(): import re from models import db_session, Country, DayData files = os.listdir('current_date_data') # last_country = session.query(Country).order_by(Country.id.desc()).first() # last_country_total_cases = last_country.total_cases # last_country_total_deaths = last_country.total_deaths with open(f'current_date_data/{files[0]}', 'r') as readfile: reader = csv.reader(readfile) for i, row in enumerate(reader): if i == 0: continue if row[file_headers[6]['index_array']['country']] == 'US': continue if row[file_headers[6]['index_array']['province']] != '': continue row[file_headers[6]['index_array']['country']] = '_'.join( row[file_headers[6]['index_array']['country']].split(' ')) print(f'i: {i}') print('name: {}'.format( row[file_headers[6]['index_array']['country']])) # print('country_name: {}'.format(row[file_headers[6]['index_array']['country']])) try: relevent_country = db_session.query(Country).filter_by( name=row[file_headers[6]['index_array'] ['country']]).first() except: if len(relevent_country.name.split(' ')) > 1: for i, country_name_part in enumerate( relevent_country.name.split(' ')): try: relevent_country = db_session.query( Country).filter_by(name=row[ file_headers[6]['index_array'] ['country']].split(' ')[i]).first() if relevent_country != None: break except: pass if relevent_country is None: continue # print(f'country_object: {dir(relevent_country)}') last_country_total_cases = relevent_country.total_cases last_country_total_deaths = relevent_country.total_deaths print(f'last_country_total_cases: {last_country_total_cases}') date = row[file_headers[6]['index_array']['last_update']].split( ' ')[0] # date = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('') # print(f'list index out of range: {date_components}') date_reformatter = lambda x: x.insert( 4, datetime.datetime.strptime(x[4].split(' ')[ 0], '%m/%d/%y').strftime('%m-%d-%Y')) if re.search( "/", x[4].split(' ')[0]) is not None else x # writeable_content = map(date_reformatter, writeable_content) from sqlalchemy import update # country = Country( # name = row[file_headers[6]['index_array']['country']], # total_cases = int(last_country_total_cases) + int(row[file_headers[6]['index_array']['confirmed']]), # total_deaths = int(last_country_total_deaths) + int(row[file_headers[6]['index_array']['deaths']]) # ) update(Country).where(Country.name == relevent_country.name).\ values( name = row[file_headers[6]['index_array']['country']], total_cases = int(last_country_total_cases) + int(row[file_headers[6]['index_array']['confirmed']]), total_deaths = int(last_country_total_deaths) + int(row[file_headers[6]['index_array']['deaths']]) ) day_data = DayData( country=relevent_country, date=datetime.date(int(date.split('-')[0]), int(date.split('-')[1]), int(date.split('-')[2])), new_cases=int( row[file_headers[6]['index_array']['confirmed']]), new_deaths=int(row[file_headers[6]['index_array']['deaths']])) print(f'country: {relevent_country} \nday_data: {day_data}') db_session.add(relevent_country) db_session.add(day_data) db_session.commit() readfile.close()