Beispiel #1
0
    def post(self):
        # create a new day
        data = request.get_json()
        if not data:
            ret = {
                'message': 'Provide data.',
            }
            return jsonify(ret), 422
        exercises = []
        if data.get('exercises'):
            exercises = [
                Exercise.get(each_exercise)
                for each_exercise in data.get('exercises')
            ]

        day = Day(name=data['name'], exercises=filter(None, exercises))
        db.session.add(day)
        db.session.commit()
        db.session.refresh(day)
        ret = {
            'message': 'Day created successfully.',
            'id': day.id,
            'name': data['name']
        }
        return jsonify(ret), 200
Beispiel #2
0
def update():
    browser = webdriver.PhantomJS(executable_path="path")
    browser.get(url)

    html = browser.page_source
    browser.close()

    content = BeautifulSoup(html, 'html5lib')
    usage = {}
    if len(content.find_all(id="bwm-daily-grid")) == 0:
        ## exit if content not found or connection error
        return False
    for row in content.find_all(id="bwm-daily-grid")[0].find_all("tr"):
        if len(row.find_all(class_="total")) == 0:
            continue  ##skip header rows or rows not containing data
        date = str(row.find_all(class_="rtitle")[0].text)
        dl = float((str(row.find_all(class_="dl")[0].text)).split(" ")[0])
        ul = float((str(row.find_all(class_="ul")[0].text)).split(" ")[0])
        total = float(
            (str(row.find_all(class_="total")[0].text)).split(" ")[0])
        usage[date] = {"download": dl, "upload": ul, "total": total}

        objs = Day.objects.all().filter(date=date)
        if (len(objs) > 0):
            obj = objs[0]
            if (obj.download != dl or obj.upload != ul or obj.total != total):
                obj.download = dl
                obj.upload = ul
                obj.total = total
                obj.save()
        else:
            obj = Day(download=dl, upload=ul, total=total, date=date)
            obj.save()
    return True
Beispiel #3
0
    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(PlotSymbolView, self).get_context_data(**kwargs)
        symbols = sim.normalize_symbols(self.kwargs['symbols'])
        df = sim.price_dataframe(symbols=symbols,
                                 start=datetime.datetime(2010, 1, 1),
                                 end=datetime.datetime(2010, 12, 31),
                                 price_type='close')
        # TODO: test!
        qs = []
        for i, symbol in enumerate(df.columns):
            for row in df.records():
                qs += [
                    Day(date=datetime.date(row[0].year, row[0].month,
                                           row[0].day),
                        close=row[i + 1],
                        symbol=symbol)
                ]
        Day.objects.bulk_create(qs)

        context['df'] = df
        return d3_plot_context(context,
                               table=df,
                               title='Price History',
                               xlabel='Date',
                               ylabel='Adjusted Close')
Beispiel #4
0
def create():
    form = AddForm(request.form)

    if form.validate_on_submit():
        day = Day(name=form.name.data)
        db.session.add(day)
        db.session.commit()
        return redirect(url_for('days.index'))

    return render_template('days/create.html', form=form)
Beispiel #5
0
def add_blank_day(event_id):
    name = request.data['name']
    info = Day(event_id=event_id,
               name=name,
               result_pdf=None,
               schedule_pdf=None)
    session.add(info)
    session.commit()
    session.close()
    return redirect('/dashboard')
    return {'status': 'OK'}
Beispiel #6
0
def populate():
    for i in range(7):
        m = Movie(
            week = i
        )
        for i in range(0,24,3):
            m.day.append(Day(
                hour = i,
                people = 0
            ))
        db.session.add(m)
        pass
            
    db.session.commit()
    return jsonify( { 'status': 'successful'} ), 200
Beispiel #7
0
def api():
    token = request.headers.get('Authorization')
    content = request.json
    if token and token.startswith('Basic') and len(content):
        fetched_content = content.pop()
        parsed_key = base64.b64decode(token.split()[1]).decode('utf-8')
        user = User.get_user(parsed_key)
        if user and fetched_content:
            payload = dict()
            payload['user_last_name'] = user.last_name
            payload_for_today = Payload.check_today(user.last_name)
            current_month = Month.check_month(user.last_name)
            if payload_for_today:
                last_time = payload_for_today.last_time
                current_time = get_time(fetched_content['time'])
                delta = hms_to_m(make_delta(last_time, current_time))
                if delta > 10:
                    payload_for_today.last_time = current_time
                    db.session.commit()
                    return Response(status='200')
                current_day = Day.get_current_day(user.last_name)
                payload['date'] = get_today()
                payload['running_min'] = delta

                if current_day:
                    current_day_running_min = current_day.running_min
                    current_day.running_min = current_day_running_min + delta
                else:
                    db.session.add(Day(**payload))

                if current_month:
                    current_month_running_min = current_month.running_min
                    current_month.running_min = current_month_running_min + delta
                else:
                    db.session.add(Month(**payload))

                payload_for_today.last_time = current_time
                db.session.commit()
            else:
                payload['last_time'] = get_time(fetched_content['time'])
                db.session.add(Payload(**payload))
                db.session.commit()
    return Response(status='200')
Beispiel #8
0
def populate_today(user):
    """
	Populate the database (`day` and `event` tables) with a list of the user's 
	events that occur today.

	:param background.models.User user: the user for which to get events
	"""
    # Add a new Day to the user's days table if it doesn't already exist
    day = user.days.filter(Day.date == TODAY).first()
    if day is None:
        day = Day(date=datetime(TODAY.year, TODAY.month, TODAY.day), user=user)
        session.add(day)
        session.commit()

    # Get today's events from WEconnect and add them to the day's events table
    activity_events = weconnect.get_todays_events(user)
    for wc_act in activity_events:
        act = user.activities.filter(
            Activity.wc_act_id == wc_act["activityId"]).first()
        for wc_ev in wc_act["events"]:
            # If the event doesn't already exist for today, add it
            event = session.query(Event).filter(
                Event.eid == wc_ev["eid"]).first()
            if event:
                modified = datetime.strptime(wc_act["dateModified"],
                                             weconnect.DATE_FMT)
                if modified >= datetime.now() - timedelta(days=1):
                    event.start_time = datetime.strptime(
                        wc_ev["dateStart"], weconnect.DATE_FMT)
                    event.end_time = act.start_time + timedelta(
                        minutes=wc_ev["duration"])
                    event.completed = wc_ev["didCheckin"]
            else:
                st = datetime.strptime(wc_ev["dateStart"], weconnect.DATE_FMT)
                et = st + timedelta(minutes=wc_ev["duration"])
                event = Event(eid=wc_ev["eid"],
                              start_time=st,
                              end_time=et,
                              completed=wc_ev["didCheckin"],
                              day=day,
                              activity=act)
                session.add(event)
    session.commit()
Beispiel #9
0
def start_before(date_before):
    # 获取日期内容
    url = 'https://news-at.zhihu.com/api/4/news/before/{}'.format(date_before)
    loop = asyncio.get_event_loop()
    task = asyncio.ensure_future(get_json_data(url))
    loop.run_until_complete(task)
    json_data = task.result()
    date = json_data['date']
    day_query = Day.query.filter_by(date=date)
    # 日期内容写入数据库
    if day_query.count() == 0:
        new_day = Day(date=date, data=json.dumps(json_data), update=datetime.now())
        db_session.add(new_day)
        db_session.commit()
    else:
        day_query.update({'data': json.dumps(json_data), 'update': datetime.now()})

    # 异步抓取文章
    tasks = [start_article(story, date) for story in json_data['stories']]
    loop.run_until_complete(asyncio.wait(tasks))
Beispiel #10
0
def add_day():

    date = request.json['date']
    breakfast = request.json['breakfast']
    lunch = request.json['lunch']
    dinner = request.json['dinner']
    rating = request.json['rating']
    gym = request.json['gym']
    notes = request.json['notes']

    day = Day(
        date = date,
        breakfast = breakfast,
        lunch = lunch,
        dinner = dinner, 
        rating = rating,
        notes = notes,
        gym = gym)

    db.session.add(day)
    db.session.commit()

    return jsonify(success=True)
Beispiel #11
0
time_h = date.hour
time_d = date.day
time_m = date.month

month = Aggregate.query.filter_by(month_number=time_m).first()

if month is None:
    # Create Month, Day, Hour
    month = Aggregate(month=date.strftime("%B"),
                      month_number=time_m,
                      average_buy=-1,
                      average_sell=-1)
    day = Day(month=date.strftime("%B"),
              month_number=time_m,
              average_buy=-1,
              average_sell=-1,
              parent_month=month,
              day_number=time_d)
    hour = Hours(hour_number=time_h, buy_price=-1, sell_price=-1, hour=day)

    db.session.add(month)
    db.session.add(day)
    db.session.add(hour)
    db.session.commit()

    buyHour(hour)
    sellHour(hour)

elif month:
    day = Day.query.filter_by(day_number=time_d, month_id=month.id).first()
for building in scrape.getBuildingsOrdered():
    b = Building.query.filter_by(name=building.name.rstrip()).first()

    for room in building.getRooms2():

        r_id = 0
        for r in b.rooms:
            if (r.roomnumber == room.number):
                r_id = r.id
                break

        for day in room.getDays2():
            day.sortTime()
            times = day.timeString()
            #times.sort()
            d = Day(name=day.day, ranges="", room_id=r_id)
            print("Adding day:", day.day)
            print("Adding times: ", times)
            d.add_time(times)
            db.session.add(d)

db.session.commit()
#             print(timeObject)
#             print(type(timeObject))
#             print(dir(timeObject))
#             print(timeObject[0].toString())

# for building in scrape.getBuildings():
#     for room in scrape.getBuildings().get(building).getRooms():
#         for day in scrape.getBuildings().get(building).getRooms().get(room).getDays():
#             print("Today's schedule for ", building,room, day)
Beispiel #13
0
def creation(date, buy, sell):
    date = strptime(date, '%m/%d/%y %H:%M')
    print("%r" % date)

    time_h = date.tm_hour
    time_d = date.tm_mday
    time_m = date.tm_mon
    complete_month = 0
    complete_day = 0

    monthDict = {
        1: 'Jan',
        2: 'Feb',
        3: 'Mar',
        4: 'Apr',
        5: 'May',
        6: 'Jun',
        7: 'Jul',
        8: 'Aug',
        9: 'Sep',
        10: 'Oct',
        11: 'Nov',
        12: 'Dec'
    }

    month = Aggregate.query.filter_by(month_number=time_m).first()
    day = Day.query.filter_by(month_number=time_m).first()

    if month is None:
        # Create Month, Day, Hour
        month = Aggregate(month=monthDict[time_m],
                          month_number=time_m,
                          average_buy=-1,
                          average_sell=-1)
        day = Day(month=monthDict[time_m],
                  month_number=time_m,
                  average_buy=-1,
                  average_sell=-1,
                  parent_month=month,
                  day_number=time_d)
        hour = Hours(hour_number=time_h,
                     buy_price=buy,
                     sell_price=sell,
                     hour=day)

        db.session.add(month)
        db.session.add(day)
        db.session.add(hour)
        db.session.commit()

        complete_month = buyMonth(month)
        sellMonth(month)

    else:
        day = Day.query.filter_by(day_number=time_d, month_id=month.id).first()

        if day is None:
            day = Day(month=monthDict[time_m],
                      month_number=time_m,
                      average_buy=-1,
                      average_sell=-1,
                      parent_month=month,
                      day_number=time_d)
            hour = Hours(hour_number=time_h,
                         buy_price=buy,
                         sell_price=sell,
                         day_id=day.id)

            db.session.add(day)
            db.session.add(hour)
            db.session.commit()

            complete_day = buyDay(day)
            sellDay(day)

        else:
            hour = Hours.query.filter_by(hour_number=time_h,
                                         day_id=day.id).first()

            if hour is None:
                hour = Hours(hour_number=time_h,
                             buy_price=buy,
                             sell_price=sell,
                             day_id=day.id)

                db.session.add(hour)
                db.session.commit()
Beispiel #14
0
def displayWeek():
    try:
        choosenDate = datetime.strptime(request.args.get('date', ''),
                                        "%Y-%m-%d").date()
    except:
        choosenDate = date.today()
    monday = getMonday(choosenDate)
    try:
        message = request.args.get('message', '')
    except KeyError:
        message = ''
    year = monday.strftime("%Y")
    monthNames = [
        "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August",
        "September", "Oktober", "November", "Dezember"
    ]
    monthNumbers = [
        "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"
    ]
    month = monthNames[monday.month - 1]
    day = monday.day
    week_number = choosenDate.isocalendar()[1]
    week = [(monday + timedelta(days=0)).day, (monday + timedelta(days=1)).day,
            (monday + timedelta(days=2)).day, (monday + timedelta(days=3)).day,
            (monday + timedelta(days=4)).day, (monday + timedelta(days=5)).day,
            (monday + timedelta(days=6)).day]
    weekdays = {
        (monday + timedelta(days=0)).day: 'Montag',
        (monday + timedelta(days=1)).day: 'Dienstag',
        (monday + timedelta(days=2)).day: 'Mittwoch',
        (monday + timedelta(days=3)).day: 'Donnerstag',
        (monday + timedelta(days=4)).day: 'Freitag',
        (monday + timedelta(days=5)).day: 'Samstag',
        (monday + timedelta(days=6)).day: 'Sonntag'
    }
    dishes = Dishes()
    daysObject = Days()
    # FEATURE: Abrufen des zugewiesenen Gerichts für den Tag
    # In der Schleife werden die dishes
    days = {}
    for day in range(0, 7):
        actualDay = monday + timedelta(days=day)
        dishesperDay = daysObject.list(actualDay)

        if len(dishesperDay) != 0:
            days[actualDay.day] = Day(year, int(actualDay.strftime("%m")),
                                      actualDay.strftime("%d"), dishesperDay,
                                      True)
        else:
            days[actualDay.day] = Day(year, int(actualDay.strftime("%m")),
                                      actualDay.strftime("%d"), dishesperDay,
                                      False)
        if actualDay == datetime.now().date():
            days[actualDay.day].isToday = True

    if message:
        flash(message)

    output = render_template(
        'index.html',
        linkCreateDish=url_for('formCreateDish'),
        linkchooseDish=url_for('formChooseDish'),
        nextWeek=(choosenDate + timedelta(days=7)).strftime("%Y-%m-%d"),
        lastWeek=(choosenDate + timedelta(days=-7)).strftime("%Y-%m-%d"),
        week_number=week_number,
        choosenDatum=monday,
        year=year,
        monthName=month,
        monthNames=monthNames,
        monthNumbers=monthNumbers,
        week=week,
        weekdays=weekdays,
        month=monday.month - 1,
        days=days,
        dishes=dishes)
    return output