Пример #1
0
async def timer_handler(message: Message):
    def check_int(number):
        return bool(re.match("^[-+]?[0-9]+$", number))

    arg = message.text.split(' ')
    if len(arg) < 2 or arg[1].replace(' ',
                                      '') == '' or check_int(arg[1]) is False:
        await message.reply(
            "Incorrect input\n"
            "/timer [mins], mins >= 0, mins <= sys.maxint",
            reply=False)
        return
    if sys.maxsize <= int(arg[1]):
        await message.reply("Number should not be larger than int",
                            reply=False)
        return
    if int(arg[1]) < 0:
        await message.reply("Number should not be less than 0", reply=False)
        return

    session = Session()
    chat = get_chat(session, message.chat.id)
    chat.default_time = int(arg[1])
    session.commit()
    session.close()

    await message.reply(f"Timer is set to {int(arg[1])} minutes", reply=False)
Пример #2
0
def update_stock_master():
    """종목 DB 갱신"""
    try:
        session = Session()
        start_time = time.time()

        stock_master = [
            stock[0] for stock in session.query(종목_마스터.거래소코드).all()
        ]

        affected_rows = 0
        for stock in krx.get_stock_list():
            if stock['short_code'].endswith(
                    '0') and stock['full_code'] not in stock_master:
                session.add(
                    종목_마스터(stock['marketName'], stock['short_code'][1:],
                           stock['codeName'], stock['full_code']))
                affected_rows += 1

        if affected_rows > 0:
            session.commit()
            slack.send_message(
                'BATCH:update_stock_master success {}건 업데이트'.format(
                    affected_rows))

        execution_time = time.time() - start_time

        logger.info('execution_time: {}'.format(execution_time))
        logger.info('{} rows added'.format(affected_rows))

    except Exception as e:
        logger.exception(e)
        slack.send_message('BATCH:update_stock_master fail {}'.format(e))
    finally:
        session.close()
Пример #3
0
def get_announcements(course, url):
    '''Gets all new announcements

    Returns a list of all new announcements.
    '''
    session = Session()
    try:
        r = s.get('https://edux.pjwstk.edu.pl/Announcements.aspx', stream=True)
        r.raise_for_status()
        new_announcements = extract_announcements(r.content)
        # All pairs of (timestamp, message) are saved to db
        # if they arent there already
        for (timestamp, message) in new_announcements:
            announcement = session.query(Announcement). \
                filter_by(course=course,
                          created_at=timestamp,
                          message=message). \
                first()
            if announcement is None:
                # This is what we care about
                announcement = Announcement(
                    course=course,
                    created_at=timestamp,
                    message=message)
                session.add(announcement)
                print u'New announcement at {0}'.format(timestamp)
                yield (timestamp, message)
        session.commit()
    except Exception:
        session.rollback()
        raise
    finally:
        session.close()
Пример #4
0
async def timer_empty_handler(message: Message):
    session = Session()
    chat = get_chat(session, message.chat.id)
    time = chat.default_time
    session.commit()
    session.close()
    await message.reply(f"Times: {time} minutes", reply=False)
Пример #5
0
def add_user():

    try:
        # generate database schema
        Base.metadata.create_all(engine)

        # create a new session
        session = Session()

        name = request.args.get('name')
        age = request.args.get('age')
        dni = request.args.get('dni')
        ctry = request.args.get('country')

        # create an employee
        user = User(name, age, dni, ctry)

        # persists data
        session.add(user)

        # commit and close session
        session.commit()
        session.close()

        return 'User added'
    except Exception as error:
        return str(error)
Пример #6
0
 def get(cls, set_code: str, number: str):
     session = Session()
     q = session.query(cls).filter(cls.set == set_code)\
                           .filter(cls.collector_number == number)
     printing_obj = q.first()
     session.close()
     return printing_obj
Пример #7
0
async def callback_add_handler(callback: CallbackQuery):
    queue_id = int(callback.data.split("-")[1])
    session = Session()
    record = session.query(QueueRecord).filter(
        QueueRecord.queue_id == queue_id,
        QueueRecord.user_id == callback.from_user.id).first()
    if record:
        session.close()
        await bot.answer_callback_query(callback.id,
                                        "You are already in the list")
        return

    queue = session.query(Queue).filter(Queue.id == queue_id).first()
    position = len(
        session.query(QueueRecord).filter(
            QueueRecord.queue_id == queue_id).all()) + 1
    user = get_user(session, callback.from_user)
    session.add(
        QueueRecord(queue_id=queue_id,
                    user_id=callback.from_user.id,
                    position=position))
    session.commit()

    text = f"{queue.title}\n\nLine:"
    for record in session.query(QueueRecord).filter(
            QueueRecord.queue_id == queue_id).all():
        text += f"\n{record.position}. {record.user.user_name}"

    await bot.answer_callback_query(callback.id, "Entered")
    await bot.edit_message_text(text,
                                queue.chat_id,
                                queue.message_id,
                                reply_markup=get_keyboard(queue))
    session.close()
Пример #8
0
def fit_process(uid):
    # create a new temporary model file
    fd, path = tempfile.mkstemp()

    # close the temporary model file descriptor as we don't need it
    os.close(fd)

    # give this process a dedicated session
    session = Session()
    try:
        ftclassifier.fit(session, uid, path)
        # sgdclassifier.fit(session, uid, path)

        # persist the model to the database
        with open(path, 'rb') as f:
            classifier = f.read()
            dal.update_classifier(session, uid, classifier)

        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        Session.remove()

    # delete the temporary model file
    os.unlink(path)
Пример #9
0
def toggle_task(update: Update, context: CallbackContext) -> None:
    user_id = update.callback_query.from_user.id
    callback_query = update.callback_query
    message_keyboard_markup = callback_query.message.reply_markup.inline_keyboard
    event, task_id = callback_query.data.split(":")

    for i in range(len(message_keyboard_markup)):
        if message_keyboard_markup[i][0].callback_data == callback_query.data:
            session = Session()

            user_task = session.query(UserTask)\
                .filter(UserTask.id == task_id)\
                .filter(UserTask.tg_user_id == user_id)\
                .first()

            if user_task is not None:
                user_task.completed = not user_task.completed
                message_keyboard_markup[i][0] = \
                    InlineKeyboardButton(format.task(user_task), callback_data=f"UserTask_toggle_click:{user_task.id}")

                session.commit()
            else:
                del message_keyboard_markup[i]
                session.close()

            break

    context.bot.edit_message_reply_markup(
        chat_id=update.effective_chat.id,
        message_id=update.callback_query.message.message_id,
        reply_markup=InlineKeyboardMarkup(message_keyboard_markup))

    update.callback_query.answer()
Пример #10
0
def insert_test_values():
    from database import Session
    from database.objects import User, Address, BlogPost, Keyword
    from random import randint

    letters = "abcdefghijklmnopqrstuvwxyz"

    s = Session()

    keyword = Keyword("firstpost")

    for n in range(100):
        name = ""
        for l in range(randint(4, 10)):
            letter = letters[randint(0, len(letters) - 1)]
            name += letter
        user = User(name=name, fullname=name, password="******")
        user.addresses = [
            Address(email_address=("*****@*****.**" % name)),
            Address(email_address=("*****@*****.**" % name)),
        ]
        post = BlogPost(("%ss Blog Post" % name), "This is a test", user)
        post.keywords.append(Keyword(name))
        post.keywords.append(keyword)
        s.add(post)

    s.commit()
    s.close()
Пример #11
0
def stats():
    """
    Get a list of all the given user's stats.
    """
    try:
        id_token = request.form['id_token']
        uid = verify_id_token(id_token)
    except KeyError:
        return "id_token required", status.HTTP_400_BAD_REQUEST
    except ValueError:
        return "id_token unrecognized", status.HTTP_400_BAD_REQUEST
    except auth.AuthError as exc:
        if exc.code == 'ID_TOKEN_REVOKED':
            return "id_token revoked", status.HTTP_400_BAD_REQUEST
        else:
            return "id_token invalid", status.HTTP_400_BAD_REQUEST
    session = Session()
    try:
        stats = dal.get_stats(session, uid)
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        Session.remove()
    return json.dumps(stats), status.HTTP_200_OK
Пример #12
0
def populate():
    """
    Populates the database for the given user with sample data.
    """
    try:
        id_token = request.form['id_token']
        uid = verify_id_token(id_token)
    except KeyError:
        return "id_token required", status.HTTP_400_BAD_REQUEST
    except ValueError:
        return "id_token unrecognized", status.HTTP_400_BAD_REQUEST
    except auth.AuthError as exc:
        if exc.code == 'ID_TOKEN_REVOKED':
            return "id_token revoked", status.HTTP_400_BAD_REQUEST
        else:
            return "id_token invalid", status.HTTP_400_BAD_REQUEST
    session = Session()
    try:
        dal.populate(session, uid)
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        Session.remove()
    classifier.fit(uid)
    return "Sample data added for user", status.HTTP_202_ACCEPTED
Пример #13
0
def reset():
    """
    Deletes all of the user's data from the database.
    """
    try:
        id_token = request.form['id_token']
        uid = verify_id_token(id_token)
    except KeyError:
        return "id_token required", status.HTTP_400_BAD_REQUEST
    except ValueError:
        return "id_token unrecognized", status.HTTP_400_BAD_REQUEST
    except auth.AuthError as exc:
        if exc.code == 'ID_TOKEN_REVOKED':
            return "id_token revoked", status.HTTP_400_BAD_REQUEST
        else:
            return "id_token invalid", status.HTTP_400_BAD_REQUEST
    session = Session()
    try:
        dal.delete(session, uid)
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        Session.remove()
    return "User data deleted", status.HTTP_202_ACCEPTED
Пример #14
0
def predict():
    """
    Predicts the text label of every value in the given list of unlabeled text.
    """
    try:
        id_token = request.form['id_token']
        uid = verify_id_token(id_token)
    except KeyError:
        return "id_token required", status.HTTP_400_BAD_REQUEST
    except ValueError:
        return "id_token unrecognized", status.HTTP_400_BAD_REQUEST
    except auth.AuthError as exc:
        if exc.code == 'ID_TOKEN_REVOKED':
            return "id_token revoked", status.HTTP_400_BAD_REQUEST
        else:
            return "id_token invalid", status.HTTP_400_BAD_REQUEST
    try:
        unlabeled_text = json.loads(request.form['unlabeled_text'])
    except KeyError:
        return "unlabeled_text required", status.HTTP_400_BAD_REQUEST
    except ValueError:
        return "unlabeled_text unrecognized", status.HTTP_400_BAD_REQUEST
    session = Session()
    try:
        predicted_labels = classifier.predict(session, uid,
                                              list(unlabeled_text.values()))
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        Session.remove()
    predictions = dict(zip(unlabeled_text.keys(), predicted_labels))
    return json.dumps(predictions), status.HTTP_200_OK
Пример #15
0
 def delete_user(self, user_id):
     s = Session()
     del_user = s.query(User).filter(User.id == user_id).one()
     s.delete(del_user)
     s.commit()
     self.signal_delete_user.emit(user_id)
     s.close()
Пример #16
0
async def callback_del_handler(callback: CallbackQuery):
    queue_id = int(callback.data.split("-")[1])
    session = Session()
    record = session.query(QueueRecord).filter(
        QueueRecord.queue_id == queue_id,
        QueueRecord.user_id == callback.from_user.id).first()
    if not record:
        session.close()
        await bot.answer_callback_query(callback.id, "You are not in the list")
        return

    user = get_user(session, callback.from_user)
    queue = session.query(Queue).filter(Queue.id == queue_id).first()
    record.remove_record()
    session.commit()

    text = f"{queue.title}\n\nLine:"
    for record in session.query(QueueRecord).filter(
            QueueRecord.queue_id == queue_id).all():
        text += f"\n{record.position}. {record.user.user_name}"

    await bot.answer_callback_query(callback.id, "Left the line")
    await bot.edit_message_text(text,
                                queue.chat_id,
                                queue.message_id,
                                reply_markup=get_keyboard(queue))
    session.close()
Пример #17
0
    def post(self):

        json_data = request.get_json()

        if not json_data:
            return {'message': 'No input data provided'}, 400

        # Validate and deserialize input
        try:
            data, errors = note_schema.load(json_data)

            if not data.get('title') or not data.get('text'):
                raise ValidationError('Data not provided')

        except ValidationError as err:
            return err.messages, 422

        db_session = Session()

        # Create new note
        note = Note(id=str(uuid.uuid4()),
                    title=data.get('title'),
                    text=data.get('text'),
                    date_create=int(time.time()),
                    date_update=int(time.time()))

        db_session.add(note)
        db_session.commit()

        result, errors = note_schema.dump(
            db_session.query(Note).filter(Note.id == note.id).first())

        db_session.close()
        return result, 201
Пример #18
0
class LeaderParser(object):
    """
    Retrieves most recently cached rankings for the specified league.
    Provides deferred functions that can be called from other Twisted
    applications.
    """
    def __init__(self):
        """Initialize a database session."""
        log.msg("Initializing Ubernet Leaderboard parser.")
        self.session = Session()

    def top(self, league):
        """Start an update and return a deferred containing the results."""
        updateDeferred = succeed(None)
        newDeferred = Deferred()

        league = league.capitalize()

        def updateDone(value):
            """Callback method for update."""
            entries = (self.session.query(
                LeaderBoardEntry.uid, UberAccount.dname).outerjoin(
                    UberAccount,
                    UberAccount.uid == LeaderBoardEntry.uid).filter(
                        LeaderBoardEntry.game == "Titans",
                        LeaderBoardEntry.league == league).order_by(
                            LeaderBoardEntry.rank))

            newDeferred.callback([e[1] for e in entries])

            self.session.close()

        updateDeferred.addCallback(updateDone)

        return newDeferred
Пример #19
0
async def create_handler(message: Message):
    title = message.text[8:]
    if title.replace(" ", "") == "":
        await message.reply("Empty title. Use /create [text]\n", reply=False)
        return
    session = Session()
    time = datetime.now()
    chat = get_chat(session, message.chat.id)
    delta = timedelta(minutes=chat.default_time)
    time += delta
    seconds = timedelta(time.second)
    time -= seconds

    queue = Queue(creator_id=message.from_user.id,
                  message_id=message.message_id,
                  pin_date=time,
                  title=title,
                  chat_id=message.chat.id)
    session.add(queue)
    session.commit()
    session.close()

    await message.reply(
        f"{title}\n\nPublication time: {time.strftime('%H:%M, %d.%m.%Y')}",
        reply=False)
Пример #20
0
def echo_controller(request):
    data = request.get('data')
    session = Session()
    message = Message(data=data.get('text'))
    session.add(message)
    session.commit()
    session.close()
    return make_response(request, 200, data)
def get_pg() -> Generator[Session, None, None]:
    db = None
    try:
        db = Session()
        yield db
    finally:
        if db:
            db.close()
Пример #22
0
def setup_base():
    Base.metadata.drop_all(bind=db, tables=[User.__table__, Address.__table__])
    Base.metadata.create_all(db)
    session = Session()
    change_user = User(name="Mike", age=44)
    session.add(change_user)
    session.commit()
    session.close()
Пример #23
0
def get_db():
    db = None
    try:
        db = Session()
        yield db
    finally:
        if db:
            db.close()
Пример #24
0
def delete_message_controller(request):
    data = request.get('data')
    message_id = data.get('message_id')
    session = Session()
    message = session.query(Message).filter_by(id=message_id).first()
    session.delete(message)
    session.commit()
    session.close()
    return make_response(request, 200)
Пример #25
0
 def add_user(self, name, address):
     if not (name == '' or address == ''):
         s = Session()
         u = User(name=name, fullname='%s test' % name, password='******')
         u.addresses = [Address(email_address=address), ]
         s.add(u)
         s.commit()
         self.send_user(u)
         s.close()
Пример #26
0
def update_message_controller(request):
    message_id = request.get('message_id')
    message_data = request.get('message_data')
    session = Session()
    message = session.query(Message).filter_by(id=message_id).first()
    message.data = message_data
    session.commit()
    session.close()
    return make_response(request, 200)
Пример #27
0
    def index(self, fullname=None, username=None, password=None):
        if cherrypy.request.method == "POST":
            session = Session()
            new_user = User(username, fullname, password)
            session.add(new_user)
            session.commit()
            session.close()

        raise cherrypy.HTTPRedirect("/")
Пример #28
0
def get_delays():
    threading.Timer(250.0, get_delays).start()
    mav_data["last_update_time"] = datetime.utcnow()
    result = {"vac_to_bp": {}, "bp_to_vac": {}}
    result["vac_to_bp"]["counter"] = 0
    result["vac_to_bp"]["minutes"] = 0
    result["vac_to_bp"]["delayed_trains"] = []
    result["bp_to_vac"]["counter"] = 0
    result["bp_to_vac"]["minutes"] = 0
    result["bp_to_vac"]["delayed_trains"] = []

    vac_to_bp = mav.MavSchedule("vác", "nyugati")
    bp_to_vac = mav.MavSchedule("nyugati", "vác")
    date_format_string = '%H:%M'

    vac_to_bp_counter = 0
    vac_to_bp_minute_counter = 0
    bp_to_vac_counter = 0
    bp_to_vac_minute_counter = 0

    session = Session()

    today_midnight_unix = datetime.combine(date.today(),
                                           datetime.min.time()).timestamp()

    trains_in_db = session.query(Train).filter(
        Train.date > today_midnight_unix)

    for train in vac_to_bp.trains:
        if train.origin_arrival != train.real_arrival and train.real_arrival is not "":
            origin = datetime.strptime(train.origin_arrival,
                                       date_format_string)
            real = datetime.strptime(train.real_arrival, date_format_string)
            if real > origin:
                add_train_to_db(session, trains_in_db, train)
                result["vac_to_bp"]["counter"] += 1
                result["vac_to_bp"]["minutes"] += int(
                    (real - origin).seconds / 60)
                result["vac_to_bp"]["delayed_trains"].append(train.serialize())

    for train in bp_to_vac.trains:
        train_in_db = False
        if train.origin_arrival != train.real_arrival and train.real_arrival is not "":
            origin = datetime.strptime(train.origin_arrival,
                                       date_format_string)
            real = datetime.strptime(train.real_arrival, date_format_string)
            if real > origin:
                add_train_to_db(session, trains_in_db, train)
                result["bp_to_vac"]["counter"] += 1
                result["bp_to_vac"]["minutes"] += int(
                    (real - origin).seconds / 60)
                result["bp_to_vac"]["delayed_trains"].append(train.serialize())

    session.commit()
    session.close()
    mav_data["delays"] = result
Пример #29
0
async def pin_switch_handler(message: Message):
    session = Session()
    chat = get_chat(session, message.chat.id)
    chat.pin = not chat.pin
    session.commit()
    if chat.pin:
        await message.reply("Pin enabled", reply=False)
    else:
        await message.reply("Pin disabled", reply=False)
    session.close()
Пример #30
0
def update_cards(verbose=True):
    session = Session()
    bulk_data_uri = scryfall.get_bulk_data()
    bulk_data = scryfall.bulk_data_generator(bulk_data_uri)
    printings = (Printing.from_scryfall(p) for p in bulk_data)
    session.add_all(printings)
    with warnings.catch_warnings():
        warnings.simplefilter('ignore', category=exc.SAWarning)
        session.commit()
    session.close()
Пример #31
0
def db_session() -> Iterator[Session]:
    session = Session()
    try:
        yield session
        session.commit()
    except Exception:
        session.rollback()
        raise
    finally:
        session.close()
def create_tasks_keyboard_markup(user_id, page_no, scope):
    session = Session()

    user_tasks = session.query(UserTask) \
        .filter(UserTask.tg_user_id == user_id) \
        .order_by(UserTask.created_at) \
        .offset(page_no * TASKS_ON_PAGE) \
        .limit(TASKS_ON_PAGE + 1) \
        .all()

    session.close()

    has_next = False
    has_prev = page_no > 0

    if len(user_tasks) > TASKS_ON_PAGE:
        user_tasks.pop()
        has_next = True

    inline_keyboard_markup = InlineKeyboardMarkup([])

    user_tasks_length = len(user_tasks)

    if user_tasks_length > 0:
        buttons = []

        for i in range(user_tasks_length):
            user_task = user_tasks[i]

            buttons.append([
                InlineKeyboardButton(
                    text=format.task(user_task),
                    callback_data=f"UserTask_{scope}_click:{user_task.id}")
            ])

        if has_prev or has_next:
            page_buttons = []

            if has_prev:
                page_buttons.append(
                    InlineKeyboardButton(
                        text=button_message.PREV_MESSAGE,
                        callback_data=f"UserTask_{scope}_page:{page_no - 1}"))

            if has_next:
                page_buttons.append(
                    InlineKeyboardButton(
                        text=button_message.NEXT_MESSAGE,
                        callback_data=f"UserTask_{scope}_page:{page_no + 1}"))

            buttons.append(page_buttons)

        inline_keyboard_markup = InlineKeyboardMarkup(buttons)

    return inline_keyboard_markup
Пример #33
0
def check_db(loud=False, delete_orphans=False, fix=False):
    session = Session()

    check_orphans(session, loud, delete_orphans)
    check_readings(session, loud, fix)
    check_masses(session, loud)
    check_events(session, loud)

    #session.commit()
    session.rollback()
    session.close()
Пример #34
0
def is_reply_queue(message: Message):
    if message.reply_to_message == None:
        return False
    session = Session()
    queue = session.query(Queue).filter(
        Queue.message_id == message.reply_to_message.message_id).first()
    if not queue:
        session.close()
        return False
    session.close()
    return True
Пример #35
0
    def get(self, list_id):
        session = Session()
        pairs = session.query(WordPair).filter(WordPair.llist_id == list_id).all()

        result = [p.as_json() for p in pairs]

        session.close()

        return result

        session.close()
Пример #36
0
def get_folders(course):
    '''Gets all folders
    '''
    session = Session()
    try:
        r = s.get('https://edux.pjwstk.edu.pl/Folder.aspx')
        r.raise_for_status()
        new_folders = extract_folders(r.content)
        for (folder_id, title, start_at, finish_at) in new_folders:
            folder = session.query(Folder). \
                filter_by(folder_id=folder_id). \
                first()
            if folder is None:
                folder = Folder(
                    folder_id=folder_id,
                    course=course,
                    title=title,
                    start_at=start_at,
                    finish_at=finish_at)
                send_notify('New folder "{}" at {}'.format(title,
                                                           course.title),
                            '''Folder title: {0.title}
Start at: {0.start_at}
Finish at: {0.finish_at}'''.format(folder))

                session.add(folder)
            if (folder.title != title or
                    folder.start_at != start_at or
                    folder.finish_at != finish_at):
                new = {
                    'title': title,
                    'start_at': start_at,
                    'finish_at': finish_at
                }
                send_notify('Folder "{0}" updated'.format(title),
                            '''Folder title: {new[title]} (old: {0.title})
Start at: {new[start_at]} (old: {0.start_at})
Finish at: {new[finish_at]} (old: {0.finish_at})'''.format(folder,
                                                           new=new))

                folder.title = title
                folder.start_at = start_at
                folder.finish_at = finish_at
                session.add(folder)
        session.commit()
    finally:
        session.close()
Пример #37
0
    def index(self, username=None):
        tableText = '<div class="container">\n\
			<table class="table">\n\
				<thead>\n\
					<tr>\n\
						<th>Name</th>\n\
						<th>Username</th>\n\
						<th>Password</th>\n\
					</tr>\n\
				</thead>\n\
				<tbody>\n'
        session = Session()
        if username == None:
            for user in session.query(User).order_by(User.id):
                tableText += (
                    "<tr>\n<td>"
                    + user.fullname
                    + "</td>\n"
                    + "<td>"
                    + user.username
                    + "</td>\n"
                    + "<td>"
                    + user.password
                    + "</td>\n</tr>\n"
                )
        else:
            for user in session.query(User).filter(User.username.like("%" + username + "%")).order_by(User.id):
                tableText += (
                    "<tr>\n<td>"
                    + user.fullname
                    + "</td>\n"
                    + "<td>"
                    + user.username
                    + "</td>\n"
                    + "<td>"
                    + user.password
                    + "</td>\n</tr>\n"
                )

        tableText += "</tbody>\n</table>\n"
        session.close()
        return top + tableText + bottom
Пример #38
0
def get_quiz(course):
    '''Navigates to quiz

    Gets all quiz
    '''
    session = Session()
    try:
        r = s.get('https://edux.pjwstk.edu.pl/Quiz.aspx')
        r.raise_for_status()

        # quiz = []

        for (quiz_id,
             title,
             start_at,
             finish_at,
             duration,
             score) in extract_quiz(r.content):
            quiz = session.query(Quiz). \
                filter_by(quiz_id=quiz_id). \
                first()
            if quiz is None:
                quiz = Quiz(
                    course=course,
                    quiz_id=quiz_id,
                    title=title,
                    start_at=start_at,
                    finish_at=finish_at,
                    duration=duration,
                    score=score
                )
                session.add(quiz)
                print u'New quiz "{0}" {1} - {2}'.format(
                    quiz.title,
                    quiz.start_at,
                    quiz.finish_at)
                send_notify(u'Quiz "{0.title}" at {1.title}'.format(quiz,
                                                                    course),
                            u'''Quiz title: {0.title}
Course: {1.title}
Start: {0.start_at}
Finish: {0.finish_at}
Duration: {0.duration}
Score: {0.score}
'''.format(quiz, course))

            if (quiz.title != title or
                    quiz.start_at != start_at or
                    quiz.finish_at != finish_at or
                    quiz.duration != duration or
                    quiz.score != score):
                send_notify(u'Quiz "{0.title}" changed'.format(quiz,
                                                               course),
                            u'''Quiz title: {new[title]} (old: {0.title})
Course: {1.title}
Start: {new[start_at]} (old: {0.start_at})
Finish: {new[finish_at]} (old: {0.finish_at})
Duration: {new[duration]} (old: {0.duration})
Score: {new[score]} (old: {0.score})
'''.format(quiz, course, new={'title': title,
                              'start_at': start_at,
                              'finish_at': finish_at,
                              'duration': duration,
                              'score': score}))
                quiz.title = title
                quiz.start_at = start_at
                quiz.finish_at = finish_at
                quiz.duration = duration
                quiz.score = score
                session.add(quiz)

        session.commit()
    except Exception:
        session.rollback()
        raise
    finally:
        session.close()
Пример #39
0
 def load_all_users(self):
     s = Session()
     for u in s.query(User).all():
         self.send_user(u)
     s.close()
Пример #40
0
def print_date(date):
    session = Session()
    ref_year = calc_ref_year(date)
    lit_year = build_dict_lit_year(ref_year, session)
    print_lit_date(lit_year[date])
    session.close()
Пример #41
0
def print_year(year):
    session = Session()
    lit_year = build_lit_year(year, session)
    for ld in lit_year:
        print_lit_date(ld)
    session.close()
Пример #42
0
def import_from_scrape(year, month):
    lit_years = {}
    session = Session()

    for day in real_itermonthdays(year, month):
        date = datetime.date(year, month, day)
        print >> sys.stderr, "Importing %s..." % (date)
        lit_date = get_lit_date(date, lit_years, session)

        # Check if we already have a mass here
        try:
            lit_date.get_masses(strict=False)
        except SelectingMassException:
            pass
        else:
            print >> sys.stderr, "  * skipping because a valid mass already exists"
            continue

        winner = lit_date.get_winner(remove_ok=True)
        if winner is None:
            print >> sys.stderr, "  * skipping because there are no masses today"
            continue

        event = winner[1]
        with open(os.path.join('scrape', '%04d-%02d-%02d.html' % (year, month, day))) as fhtml:
            quotes = scrape_file(fhtml)

        if u'auto' not in event.status.split(u' '):
            event.status += u' auto'

        mass = Mass()
        mass.order = 0
        mass.event = event
        mass.digit = lit_date.digit
        mass.letter = lit_date.letter
        mass.title = None
        mass.status = u'auto'
        session.add(mass)

        order = 0
        if len(quotes) == 4:
            titles = [u'Prima lettura', u'Salmo responsoriale', u'Seconda lettura', u'Vangelo']
        elif len(quotes) == 3:
            titles = [u'Prima lettura', u'Salmo responsoriale', u'Vangelo']
        # Domenica delle Palme
        elif len(quotes) == 5:
            titles = [u'Vangelo delle Palme', u'Prima lettura', u'Salmo responsoriale', u'Seconda lettura', u'Vangelo']
        # Pasqua
        elif len(quotes) == 17:
            titles = [u'Prima lettura',
                      u'Salmo responsoriale',
                      u'Seconda lettura',
                      u'Salmo responsoriale',
                      u'Terza lettura',
                      u'Salmo responsoriale',
                      u'Quarta lettura',
                      u'Salmo responsoriale',
                      u'Quinta lettura',
                      u'Salmo responsoriale',
                      u'Sesta lettura',
                      u'Salmo responsoriale',
                      u'Settima lettura',
                      u'Salmo responsoriale',
                      u'Epistola',
                      u'Salmo responsoriale',
                      u'Vangelo']
        else:
            raise Exception('Strange number of readings (%d)' % (len(quotes)))

        for (quote, text), title in zip(quotes, titles):
            reading = Reading()
            reading.order = order
            order += 1
            reading.alt_num = 0
            reading.mass = mass
            reading.title = title
            reading.quote = canonicalise_quote(quote)
            reading.text = text
            try:
                decode_quote(quote, allow_only_chap=True, valid_abbr=ABBR_VATICAN)
            except:
                reading.quote_status = u'auto invalid'
            else:
                reading.quote_status = u'auto'
            if text is None:
                reading.text_status = u'missing'
            else:
                reading.text_status = u'auto'
            session.add(reading)

        session.flush()

        # Write some interesting things
        #print '#'
        #print_lit_date(lit_date, PrependStream(sys.stdout, '# '))
        #print
        #print json.dumps(event.as_dict(), encoding='utf-8', ensure_ascii=False, indent=2, sort_keys=True)
        #print

    session.commit()
    session.close()