示例#1
0
文件: views.py 项目: woolfius/placard
def upload_file():
    """
    Збереження файлів в БД.
    """
    MAX_FILE_SIZE = 4024 * 4024 + 1
    if request.method == 'POST':
        file = request.files["file"]
        print(request.files)
        if bool(file.filename):
            file_bytes = file.read(MAX_FILE_SIZE)
            gif_str = base64.b64encode(file_bytes)
            id_ = request.form['id']
            print(id_)
            url = request.form['redirect']
            print(url)

            type = request.form['type']
            date1 = datetime.strftime(datetime.now(), "%Y.%m.%d")
            name = request.form['name']
            print(name)
            session = Session()
            documents = Document(name=name,
                                 file_type=file.mimetype,
                                 file=gif_str,
                                 type_=type,
                                 fk_person=id_)
            session.add(documents)
            session.commit()
            session.close()
            return redirect('/#!' + url + '#' + request.form['part'])
    return 'ok'
示例#2
0
 def add(self, message, latitude, longitude, time_limit=60):
     post = Post(message, latitude, longitude, time_limit)
     session = Session()
     session.add(post)
     session.commit()
     session.close()
     return post
示例#3
0
def post_links():
    session = Session()
    data = json.loads(request.data)

    for link in data['links']:
        post = Post.get_or_create(session, link['reddit_id'])
        subreddit = Subreddit.get_or_create(session, link['sub_name'])
        user = User.get_or_create(session, link['authorname'])

        post.subreddit_id   = subreddit.id
        post.author_id      = user.id

        post.domain = link['domain']
        post.title  = link['title']
        post.url    = link['url']
        post.score  = link['score']
        post.downs  = link['downs']
        post.ups    = link['ups']

        post.is_self = link['is_self']
        post.over_18 = link['over_18']
        post.thumbnail = link['thumbnail']
        post.created = float(link['created'])
        post.scraped_time = Util.now()

        session.add(post)

    session.commit()
    session.close()
    return jsonify({'received':True})
示例#4
0
def put_batch():
    session = Session()
    data = json.loads(request.data)
    worker = data['id']
    users  = data['users']
    users = filter(lambda u: u is not None, users)
    usernames = [user['name'] for user in users]

    for lock in session.query(UserLock).join(User) \
        .filter(User.name.in_(usernames), UserLock.status == UserLock.STATUS_WIP, UserLock.locker == worker):
        for u in users:
            if u['name'] == lock.user.name:
                if 'error' in u:
                    eId = str(uuid4())
                    lock.user.reddit_id = eId
                else:
                    lock.user.update_from_json(u)
                session.add(lock.user)

    session.commit()

    session.query(UserLock).join(User) \
        .filter(User.reddit_id != None, User.name.in_(usernames), UserLock.status == UserLock.STATUS_WIP, UserLock.locker == worker) \
        .update({'status': UserLock.STATUS_DONE, 'locker': None}, synchronize_session='fetch')

    session.commit()
    session.close()

    return jsonify({'received':'true'})
示例#5
0
def create_item(request, redirect_url, user_id):
    """Creates a new item and add to database"""

    # Check if form is complete
    check_item_form_data(request, redirect_url)

    # Create DB session
    db_session = Session()

    # Add new item to database
    try:
        new_item = Item(title=request.form.get("title"),
                        description=request.form.get("description"),
                        category_id=request.form.get("category"),
                        owner_id=user_id)
        db_session.add(new_item)
        db_session.commit()
    except exc.SQLAlchemyError:
        flash("There was an error. Please try again.", "danger")
        return redirect(redirect_url)

    # Close DB session
    db_session.close()

    flash("Item added!", "success")
示例#6
0
    def insert(user_id, file_name, file_size):
        try:
            session_obj = Session()
            tmp = session_obj.query(user_info).filter_by(id=user_id).first()
            if (tmp == None):
                Session.remove()
                return -1

            tmp = session_obj.query(file_list).filter_by(
                user_id=user_id).filter_by(file_name=file_name).first()
            if (tmp != None):
                Session.remove()
                return -2
            time = datetime.datetime.now()
            list_obj = file_list(user_id=user_id,
                                 file_name=file_name,
                                 file_size=file_size,
                                 time=time)
            session_obj.add(list_obj)
            session_obj.commit()
            Session.remove()

            return 0
        except Exception as e:
            session_obj.rollback()
            Logger().get_logger().error(str(e))
            return -3
示例#7
0
def send_stats():
    data = request.json

    if not is_auth(request.headers):
        return 'not authenticated', 401

    patient_id = None
    time = None
    moves = None
    answers_right = None
    answers_wrong = None

    try:
        time = data['time']
        answers_right = data['answers_right']
        answers_wrong = data['answers_wrong']
        patient_id = Cache[request.headers['Token']]['id']
    except:
        return 'invalid parameters', 400

    t = Stats(time=time,
              answers_wrong=answers_wrong,
              answers_right=answers_right,
              patient_id=patient_id)
    s = Session()
    s.add(t)
    s.commit()
    return 'OK'
示例#8
0
文件: views.py 项目: woolfius/placard
def upload_photo():
    """
    Збереження файлів в БД.
    """
    MAX_FILE_SIZE = 4024 * 4024 + 1
    if request.method == 'POST':
        file = request.files["file"]
        print(file)
        if bool(file.filename):
            file_bytes = file.read(MAX_FILE_SIZE)
            gif_str = base64.b64encode(file_bytes)
            id_ = request.form['id']
            print(id_)
            type = 'photo'
            date1 = datetime.strftime(datetime.now(), "%Y.%m.%d")
            name = 'фотографія'
            session = Session()
            documents = Document(name=name,
                                 file_type=file.mimetype,
                                 file=gif_str,
                                 type_=type,
                                 fk_person=id_)
            session.add(documents)
            session.commit()
            session.close()
            print('hello')
            return redirect('/#!/employees/new/')
            # return 'ok'
    return 'ok'
示例#9
0
def create_user(login_session, redirect_url):
    """Creates a new user and add to database"""

    # Create db_session
    db_session = Session()

    # Check to see if user exists in database and add it otherwise
    user_id = get_user_id(login_session['profile']['username'])

    if not user_id:
        # Add user to database
        try:
            new_user = User(username=login_session['profile']['username'])
            db_session.add(new_user)
            db_session.commit()
            flash("User created", "success")
        except exc.SQLAlchemyError:
            flash("Failed to create user", "danger")
            return redirect(redirect_url)

    # Query created user
    user = db_session.query(User).filter_by(
        username=login_session['profile']['username']).one()

    # Close DB session
    db_session.close()

    return user.id
示例#10
0
    def group_alias(login_id, group_name):
        try:
            session_obj = Session()
            ret = session_obj.query(user_info).filter_by(id=login_id).first()
            if not ret:
                Session.remove()
                return (-1, '')

            ret = session_obj.query(group_alias_info).filter_by(
                user_id=login_id).filter_by(name=group_name).first()
            if ret is None:
                alias_obj = group_alias_info(user_id=login_id, name=group_name)
                session_obj.add(alias_obj)
                session_obj.commit()
                Session.remove()
                return (0, '')

            if ret.alias is None:
                Session.remove()
                return (0, '')
            Session.remove()
            return (0, ret.alias)

        except Exception as e:
            Logger().get_logger().error(str(e))
            session_obj.rollback()
            return (-2, '')
示例#11
0
def read_item(shopping: UserResp):
    aUser = User(username=shopping.username, password=shopping.password)
    session = Session()
    session.add(aUser)
    session.commit()
    session.close()
    return
示例#12
0
    def on_post(self, req, resp):
        doc = req.context['doc']
        lat = doc['lat']
        lon = doc['lon']
        date_in = datetime.datetime.utcnow()
        if lat and lon:
            user = get_user(req, resp)

        if lon >= -79.8921061:
            if lon <= -79.8833942:
                if lat <= 36.0984408:
                    if lat >= 36.0903956:
                        signin = Signin(date_in=date_in, user=user)
                        user.signedin = True
                        Session.add(signin)
                        Session.commit()

                        resp.status = falcon.HTTP_201
                        resp.location = '/signins/%s' % (signin.id)
                        req.context['result'] = {"action": "sign in", "result": "success"}
            else:
                resp.status = falcon.HTTP_409
                req.context['result'] = {"action": "sign in", "result": "failure"}

        else:
            resp.status = falcon.HTTP_409
            req.context['result'] = {"action": "sign in", "result": "failure"}
示例#13
0
def upsert_bank(jurisdiction_id, bank_code=None, name=None, fetched=False):
    if bank_code not in swift_banks:
        if is_blank(name):
            name = None
        if not jurisdiction_id:
            jurisdiction_id = jurisdiction_by_code("XX")

        s = Session()
        bank = _get_bank(s, jurisdiction_id, bank_code)
        if not bank:
            bank = Bank(code=bank_code,
                        name=name,
                        country_id=jurisdiction_id,
                        fetched=fetched)
            s.add(bank)
            cash_account = Account(
                code=None, acc_type="CASH", bank=bank, fetched=True
            )  # CASH accounts don't really exist, so all pre-fetched
            s.add(cash_account)
        elif name:
            if not bank.name:
                bank.name = name
            elif bank.name != name:
                print("Bank with different name: old: %s; new: %s"\
                 %(bank.name, name))
                if len(name) > len(bank.name):
                    bank.name = name

        s.commit()
        swift_banks[bank_code] = bank.id
        s.close()

    return swift_banks[bank_code]
示例#14
0
def add_todo():
    data = request.json

    if not is_auth(request.headers):
        return 'not authenticated', 401

    family_id = None
    patient_id = None
    todo = None

    try:
        family_id = Cache[request.headers['Token']]['family_id']
        patient_id = data['patient_id']
        todo = data['todo']
    except:
        return 'invalid parameters', 400

    t = Todo(desc=todo,
             done=False,
             patient_id=patient_id,
             caretaker_id=Cache[request.headers['Token']]['id'])
    s = Session()
    s.add(t)
    s.commit()
    return 'OK'
示例#15
0
def update_user_db_ad():
    """
    Доповнення БД даними з АД
    """
    session = Session()
    records = session.query(Worker.sid,
                            Worker.fk_person).filter(Worker.sid != '').all()
    for arg in records:
        user = []
        res = ad.search(search_base=BASE_DN,
                        search_filter="(objectSid={sid})".format(sid=arg[0]),
                        attributes=[
                            'mail', 'sn', 'givenName', 'pager',
                            'thumbnailPhoto', 'mobile', 'givenName-En',
                            'givenNameUa', 'cityUa', 'ipPhone', 'l',
                            'givenName-En', 'snEn', 'givenNameUa', 'snUa',
                            'objectSid'
                        ])

        if res:
            for entry in ad.response:
                if 'dn' in entry:
                    user.append(dict(entry['attributes']))
        if user:
            if user[0]['thumbnailPhoto']:
                byte_string = user[0]['thumbnailPhoto']
                encoded_data = base64.b64encode(byte_string)
                photo = Document(name='фотографія',
                                 file_type='',
                                 file=encoded_data,
                                 type_='',
                                 fk_person=arg[1])
                session.add(photo)
            for resp in user[0]:
                if not user[0][resp]:
                    user[0][resp] = ''
            session.query(Person).filter_by(id=arg[1]).update({
                "email":
                user[0]['mail'],
                "skype":
                user[0]['pager']
            })
            session.query(Worker).filter_by(fk_person=arg[1]).update({
                "name_en":
                user[0]['givenName-En'],
                "name_ru":
                user[0]['givenName'],
                "surname_en":
                user[0]['snEn'],
                "surname_ru":
                user[0]['sn'],
                "skype":
                user[0]['pager'],
                "ip_phone":
                user[0]['ipPhone'],
                "email":
                user[0]['mail']
            })
    session.commit()
    session.close()
示例#16
0
    def create(self, model):
        session = Session()

        session.add(model)
        session.commit()

        session.close()
示例#17
0
def register_room(message, project_identifier):
    """RedmineのプロジェクトとSlackチャンネルを繋ぐ.

    :param message: slackbotの各種パラメータを保持したclass
    :param project_identifier: redmineのプロジェクトの識別名
    """
    s = Session()
    channel = message.channel
    channel_id = channel._body['id']

    user = user_from_message(message, s)
    if not user:
        return
    project, project_channel = project_channel_from_identifier(
        user.api_key, project_identifier, s)
    if not project:
        botsend(message, PROJECT_NOT_FOUND)
        return

    if not project_channel:
        project_channel = ProjectChannel(project_id=project.id)
        s.add(project_channel)

    channels = project_channel.channels.split(
        ",") if project_channel.channels else []

    if channel_id not in channels:
        channels.append(channel_id)
        project_channel.channels = ",".join(channels)
        s.commit()
        botsend(message, CHANNEL_REGISTERED.format(project.name))
    else:
        botsend(message, CHANNEL_ALREADY_REGISTERED)
示例#18
0
def vacation_user(_id):
    """
    Нарахування відпустки працівнику за діючий рік якщо він прийнятий на роботу цього ж року
    :param _id: ід працівника
    """
    session = Session()
    datenow = datetime.strftime(datetime.now(), "%Y-%m-%d")
    date2 = datetime.strptime(str(datenow), "%Y-%m-%d").date()
    result = session.query(
        Worker.id, Worker.started_to_work, Worker.finished_to_work).filter(
            Worker.status == 'active').filter(Worker.id == _id).first()
    date = str(result[1].year) + '-12-31'
    date1 = datetime.strptime(str(date), "%Y-%m-%d").date()
    if result[1].year == date1.year:
        if result[1].day < 10:
            s1 = 2
        elif result[1].day < 21:
            s1 = 1
        else:
            s1 = 0
        # кількість заробленої відпустки за цей рік
        count = s1 + 2 * (date1.month - result[1].month)
        vacation_new = Vacation(count=count,
                                used=0,
                                balance=count,
                                year=date2.year,
                                updated=date2,
                                fk_worker=result[0])
        session.add(vacation_new)
    session.commit()
    session.close()
    return count
示例#19
0
def vacation():
    """
    Заповнення відпусти за поточний рік всім працівникам
    """
    session = Session()
    date2 = datetime.strftime(datetime.now(), "%Y-%m-%d")
    date1 = datetime.date(datetime.now())
    result = session.query(
        Worker.id, Worker.started_to_work,
        Worker.finished_to_work).filter(Worker.status == 'active').all()
    for arg in result:
        if date1.year != arg[1].year and not session.query(Vacation).filter(
                Vacation.year == date1.year).filter(
                    Vacation.fk_worker == arg[0]).first():
            # Додати період +24 за поточний рік
            vacation_new = Vacation(count=24,
                                    used=0,
                                    balance=24,
                                    year=date1.year,
                                    updated=date2[0:10],
                                    fk_worker=arg[0])
            session.add(vacation_new)
            # нарахування відпустки якщо людина була прийнята на роботу цього року
        elif not session.query(Vacation).filter(
                Vacation.year == date1.year).filter(
                    Vacation.fk_worker == arg[0]).first():
            vacation_user(arg[0])
    session.commit()
    session.close()
示例#20
0
    def add_style():
        if request.form:

            name = request.form.get('name', '')

            s = Session.query(WineStyle).filter_by(name=name).first()
            if not s:
                desc = request.form.get('description', '')
                s = WineStyle(name=name, description=desc)
                Session.add(s)

            wineries_id = request.form.get('wineries', '').split(',')
            s.wineries = []
            for _id in wineries_id:
                w = Session.query(Winery).filter_by(id=_id.strip()).first()
                if w:
                    s.wineries.append(w)

        try:
            Session.commit()
        except Exception as e:
            print('unable to add WineStyle')
            print(e)

        return style(s.id)
示例#21
0
class TestItem(TestCase):
    def setUp(self):
        reset_db()
        self.sess = Session()

    def tearDown(self):
        self.sess.close()

    def test_crud(self):
        store = StoreModel('testStore')
        item = ItemModel(name='test', price=12.99, store_id=1)
        self.assertEqual(
            self.sess.query(ItemModel).filter(
                ItemModel.name == 'test').count(), 0)

        self.sess.add(store)
        self.sess.add(item)
        self.sess.commit()
        self.assertEqual(
            self.sess.query(ItemModel).filter(
                ItemModel.name == 'test').count(), 1)

        self.sess.delete(item)
        self.sess.commit()
        self.assertEqual(
            self.sess.query(ItemModel).filter(
                ItemModel.name == 'test').count(), 0)
        self.sess.delete(store)
        self.sess.commit()
示例#22
0
def main(notify):
    session = Session()
    gen = session.query(User) \
        .join(UserMeta) \
        .filter(User.reddit_id != None, \
            UserMeta.has_public_likes == UserMeta.YES, \
            UserMeta.is_active != UserMeta.NO)

    for user in gen:
        try:
            praw_user = r.get_redditor(user.name)
            user.update_from_praw(praw_user)

            for praw_post in praw_user.get_liked(limit=100):
                update(session, user, praw_post, 1)

            for praw_post in praw_user.get_disliked(limit=100):
                update(session, user, praw_post, -1)

            user.meta.has_public_likes = UserMeta.YES
        except praw.requests.exceptions.HTTPError as e:
            print str(e)
            if '403' in str(e):
                user.meta.has_public_likes = UserMeta.NO
            elif '404' in str(e):
                user.meta.is_active = UserMeta.NO

        session.add(user.meta)
        session.add(user)
        session.commit()

    session.close()
示例#23
0
    def upload(user_id, package_name, version, time, remark):
        try:
            session_obj = Session()
            tmp = session_obj.query(user_info).filter_by(id=user_id).first()
            if (tmp == None):
                Session.remove()
                return -1

            tmp = session_obj.query(package_info).options(
                subqueryload(package_info.user)).filter_by(
                    package_name=package_name).filter_by(
                        user_id=user_id).first()
            if tmp is not None:
                tmp = session_obj.query(package_info).get(tmp.id)
                session_obj.delete(tmp)
                session_obj.commit()

            package_obj = package_info(user_id=user_id,
                                       package_name=package_name,
                                       version=version,
                                       time=time,
                                       remarks=remark)
            session_obj.add(package_obj)
            session_obj.commit()
            Session.remove()

            return 0
        except Exception as e:
            Logger().get_logger().error(str(e))
            session_obj.rollback()
            return -2
示例#24
0
def branch_list():
    """
    Заповнення філіалу в БД
    """
    users_list = []
    if load_users_from_ad():
        process_ad_response(ad.response, users_list,
                            (('userAccountControl', [], False),
                             ('company', [], False)))
        session = Session()
        for arg in users_list:
            if not session.query(Branch).filter(
                    Branch.name == arg['company']).all():
                fk_city = session.query(
                    City.id).filter(City.name == arg['cityUa']).first()
                branch = Branch(name=arg['company'],
                                name_en=arg['companyEn'],
                                zip_code=arg['postalCode'],
                                address_ua=arg['streetAddressUa'],
                                address_en=arg['street'],
                                address_ru=arg['streetAddress'],
                                fk_city=fk_city[0])
                session.add(branch)
        session.commit()
        session.close()
示例#25
0
    def _update_task_list(self, limit=10, st=0, ignore=False):
        session = Session()
        tasks = self.xunlei.get_task_list(limit, st)
        for task in tasks[::-1]:
            if task['status'] == "finished":
                self.last_task_id = task['task_id']
            db_task_status = session.query(
                db.Task.status).filter(db.Task.id == task['task_id']).first()
            if db_task_status and db_task_status[0] == "finished":
                continue

            db_task = db.Task()
            db_task.id = task['task_id']
            db_task.create_uid = self.uid
            db_task.cid = task['cid']
            db_task.url = task['url']
            db_task.lixian_url = task['lixian_url']
            db_task.taskname = task['taskname']
            db_task.task_type = task['task_type']
            db_task.status = task['status']
            if db_task.status == "failed":
                db_task.invalid = True
            db_task.process = task['process']
            db_task.size = task['size']
            db_task.format = task['format']

            db_task = session.merge(db_task)
            if not self._update_file_list(db_task):
                db_task.status = "failed"
                db_task.invalid = True
                session.add(db_task)

        session.commit()
示例#26
0
def update_item(item_id, request, redirect_url):
    """Update item in database"""

    # Check if form is complete
    check_item_form_data(request, redirect_url)

    # Create DB session
    db_session = Session()

    # Query item in database by id
    item = db_session.query(Item).filter_by(id=item_id).one()

    # Update corresponding item in database
    try:
        item.title = request.form.get("title")
        item.description = request.form.get("description")
        item.category_id = request.form.get("category")
        item.last_updated = datetime.utcnow()
        db_session.add(item)
        db_session.commit()
    except exc.SQLAlchemyError:
        flash("There was an error. Please try again.", "danger")
        return redirect(redirect_url, item_id=item_id)

    # Close DB session
    db_session.close()
示例#27
0
def log(user, message):
    session = Session()
    message = AuditLogMessage(user=user,
                              message=message,
                              player_name=PLAYER_NAME)
    session.add(message)
    session.commit()
示例#28
0
    def on_post(self, req, resp):
        doc = req.context['doc']

        title = doc['title']
        description = doc['description']
        from_date = doc['from_date']
        to_date = doc['to_date']

        from_date_datetime = datetime.datetime.strptime(from_date, "%Y-%m-%d")
        to_date_datetime = datetime.datetime.strptime(to_date, "%Y-%m-%d")

        user = get_user(req, resp)

        event = Event(from_date=from_date_datetime, to_date=to_date_datetime, title=title,
                description=description, user=user)
        Session.add(event)
        Session.commit()

        resp.status = falcon.HTTP_201
        req.context['result'] = {"action": "add event", "result": "success",
                "event": {
                    'id': event.id,
                    'from_date': from_date,
                    'to_date': to_date,
                    'title': event.title,
                    'description': event.description
                }
            }
示例#29
0
    def _update_tasks(self, tasks):
        session = Session()
        nm_list = []
        bt_list = []
        for task in tasks:
            if task.task_type in ("bt", "magnet"):
                bt_list.append(task.id)
            else:
                nm_list.append(task.id)

        for res in self.xunlei.get_task_process(nm_list, bt_list):
            task = self.get_task(res['task_id'])
            task.status = res['status']
            if task.status == "failed":
                task.invalid = True
            task.process = res['process']
            if res['cid'] and res['lixian_url']:
                task.cid = res['cid']
                task.lixian_url = res['lixian_url']

            task = session.merge(task)
            if task.status in ("downloading", "finished"):
                if not self._update_file_list(task):
                    task.status = "downloading"
                    session.add(task)
        session.commit()
示例#30
0
def cleaning_add(message, user_name, day_of_week):
    """指定した曜日の掃除当番にユーザーを追加する

    :param message: slackbot.dispatcher.Message
    :param str user_name: 掃除当番に登録するユーザー名
    :param str day_of_week: 追加する掃除曜日
    """
    if day_of_week not in DAY_OF_WEEK:
        botsend(message, '曜日には `月` 、 `火` 、 `水` 、 `木` 、 `金` のいずれかを指定してください')
        return

    s = Session()
    slack_id = get_slack_id(s, user_name)
    if not slack_id:
        botsend(message, '{}はSlackのユーザーとして存在しません'.format(user_name))
        return

    q = s.query(Cleaning).filter(Cleaning.slack_id == slack_id)
    if s.query(q.exists()).scalar():
        botsend(message, '{}は既に登録されています'.format(user_name, day_of_week))
        return

    s.add(
        Cleaning(slack_id=slack_id,
                 day_of_week=DAY_OF_WEEK.index(day_of_week)))
    s.commit()
    botsend(message, '{}を{}曜日の掃除当番に登録しました'.format(user_name, day_of_week))
示例#31
0
def import_json(json):
    session = Session()
    for item_json in json['items']:
        positions = []

        for p in item_json['positions']:
            dt = datetime_from_json(p, 'datetime', datetime.now())
            positions.append(
                Position(
                    name=p['name'],
                    description=p['description'],
                    datetime=dt,
                    created=datetime_from_json(p, 'created', dt),
                    updated=datetime_from_json(p, 'updated', dt),
                ))

        now = datetime.now()

        item = Item(
            name=item_json['name'],
            description=item_json['description'],
            tags=get(item_json, 'tags', ''),
            image_filename=item_json['image_filename'],
            created=datetime_from_json(item_json, 'created', now),
            updated=datetime_from_json(item_json, 'updated', now),
            positions=positions,
        )
        session.add(item)

    session.commit()
示例#32
0
    def _update_task_list(self, limit=10, st=0, ignore=False):
        session = Session()
        tasks = self.xunlei.get_task_list(limit, st)
        for task in tasks[::-1]:
            if len(self.task_id_sample) < TASK_ID_SAMPLE_SIZE and task['lixian_url']:
                self.task_id_sample.add(task['task_id'])
            if not self.last_task_id and task['lixian_url']:
                self.last_task_id = task['task_id']
            db_task_status = session.query(db.Task.status).filter(
                    db.Task.id == task['task_id']).first()
            if db_task_status and db_task_status[0] == "finished" and self.last_task_id:
                continue

            db_task = db.Task()
            db_task.id = task['task_id']
            db_task.create_uid = self.uid
            db_task.cid = task['cid']
            db_task.url = task['url']
            db_task.lixian_url = task['lixian_url']
            db_task.taskname = task['taskname']
            db_task.task_type = task['task_type']
            db_task.status = task['status']
            if db_task.status == "failed":
                db_task.invalid = True
            db_task.process = task['process']
            db_task.size = task['size']
            db_task.format = task['format']

            db_task = session.merge(db_task)
            if not self._update_file_list(db_task):
                db_task.status = "failed"
                db_task.invalid = True
                session.add(db_task)
            
        session.commit()
示例#33
0
    def _update_tasks(self, tasks):
        session = Session()
        nm_list = []
        bt_list = []
        for task in tasks:
            if task.task_type in ("bt", "magnet"):
                bt_list.append(task.id)
            else:
                nm_list.append(task.id)

        for res in self.xunlei.get_task_process(nm_list, bt_list):
            task = self.get_task(res['task_id'])
            task.status = res['status']
            if task.status == "failed":
                task.invalid = True
            task.process = res['process']
            if res['cid'] and res['lixian_url']:
                task.cid = res['cid']
                task.lixian_url = res['lixian_url']

            task = session.merge(task)
            if task.status in ("downloading", "finished"):
                if not self._update_file_list(task):
                    task.status = "failed"
                    task.invalid = True
                    session.add(task)
        session.commit()
def process_command_output(queue):
    # TODO: run the command and put its data in the db
    command = queue.get()
    start_time = time.time()
    try:
        proc = Popen(command,
                     shell=True,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE,
                     preexec_fn=os.setsid)
        longer_exec_command = False
        #For longer running commands
        while proc.poll() is None:
            exec_time = time.time() - start_time
            if exec_time > 60:
                #kill the command execution
                os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
                longer_exec_command = True
        stdout, stderr = proc.communicate()

        duration = 0 if longer_exec_command else time.time() - start_time
        length = len(command)
        output = stdout if not stderr else stderr

        cmd_metadata = Command(command, length, duration, output)
        dbsession = Session()
        dbsession.add(cmd_metadata)
        dbsession.commit()
    except Exception as e:
        raise Exception("DB insertion failed...")
示例#35
0
def upsert_organisation(name, core=None, fetched=False):
	"""Includes normalisation
	Update not implemented
	"""
	s = Session()
	name = clean_name(name)
	org = _organisation_by_name(s, name)
	if org:
		#if name_norm != None and name_norm != org.name_norm:
		#	print("Organisation %s with different normalised name: old: '%s'; new: '%s'"\
		#		%(name, org.name_norm, name_norm))
		# if jurisdiction != None and jurisdiction != org.jurisdiction:
		#	print("Organisation %s with different jurisdiction: old: '%s'; new: '%s'"\
		#		%(name, org.jurisdiction, jurisdiction))
		# if org_type != None and org_type != org.org_type:
		# 	print("Organisation %s with different type: old: '%s'; new: '%s'"\
		# 		%(name, org.org_type, org_type))
		if core != None and core != org.core:
			print("Organisation %s with different core: old: '%s'; new: '%s'"\
				%(name, org.core, core))
	else:
		org = Organisation(name=name, core=core, fetched=fetched)
		s.add(org)
		s.commit()
	result = org.id
	s.close()
	return result
示例#36
0
def upsert_alias(name, org_id, jurisdiction_id):
	"""Atomic operation
	includes commit
	does not include normalisation 
	"""
	s = Session()
	name = clean_name(name)
	if is_blank(name) or not org_id:
		return None
	alias = _get_alias(s, name, org_id, jurisdiction_id)
	if not alias:
		alias = Alias(alias=name, org_id=org_id, country_id=jurisdiction_id)
		s.add(alias)

	company = _get_organisation(s, org_id)
	if not company:
		# TODO: What do we do with anonymous entities? E.g. cash sources
		raise Exception("Expected company with id=%d but not found"%org_id)

	# Out because names are processed after import is complete
	# if len(name) < len(company.name):
	# 	company.name = name

	s.commit()
	result = alias.id
	s.close()
	return result
示例#37
0
def load_actions(json_data):
    data = json.loads(json_data)

    items = data["items"]
    items.remove({})

    session = Session()

    for item in items:
        action = action_from_json(item)

        if action and not _sgx_key_exists(session, action.sgx_key):
            security_query = session.query(SgxActionSecurity).filter(SgxActionSecurity.company_name == action.company_name)
            if (security_query.count() == 0):
                # New company_name
                security = SgxActionSecurity(company_name=action.company_name)
                session.add(security)
            else:
                security = security_query.one()
                if (security.symbol != None):
                    action.symbol = security.symbol
            session.add(action)
            print "Added %s" % action

    session.commit()
示例#38
0
    def add_winery():
        if request.method == 'POST':
            if request.form:

                name = request.form.get('name', '')

                w = Session.query(Winery).filter_by(name=name).first()
                if not w:

                    address = request.form.get('address', '')
                    phone = request.form.get('phone', '')
                    url = request.form.get('url', '')
                    desc= request.form.get('description', '')

                    try:
                        w = Winery(name=name,
                                   address=address,
                                   phone=phone,
                                   url=url,
                                   description=desc)

                        Session.add(w)
                        Session.commit()
                    except Exception as e:
                        print('unable to add winery')
                        print(e)

                    return winery(w.id)
                else:
                    print('found winery with the same name')
            return redirect('/')
        else:
            return render_template('wineryformpage.html')
示例#39
0
def submit_person():
	session = Session()

	person = None

	if 'id' in request.form:
		person = session.query(Person).filter(Person.id == request.form['id']).first()
	else:
		person = Person()

	person.name = request.form['name']
	if 'location' in request.form:
		person.location = request.form['location']

	if 'image_id' in request.form and request.form['image_id']:
		person.image_id = request.form['image_id']
	elif('cover' in request.files) and request.files['cover'] and request.files['cover'].filename:
		upload = request.files['cover']
		file = File()

		file.name = secure_filename(upload.filename)
		file.type = 'image'
		file.mime = guess_type(file.name)[0]
		file.data = upload.read()

		person.image = file

	session.add(person)

	session.commit()

	return redirect(url_for('index'))
def main(notify):

    session = Session()
    total = session.query(Subreddit).count()
    count = 0

    notify("starting update of %d subs" % total)

    query   = session.query(Subreddit).order_by("scraped_time asc")
    dbi     = DBIterator(query=query, use_offset=None)

    for subreddit in dbi.results_iter():

        count += 1

        try:
            subreddit.update_from_praw(r.get_subreddit(subreddit.url.split('/')[2]))
            session.add(subreddit)

        except (praw.requests.exceptions.HTTPError, praw.errors.InvalidSubreddit) as e:
            print "ERROR", str(e)
            subreddit.touch()
            session.add(subreddit)

        if count % 2000 == 0 and notify is not None:
            notify("at %d of %d" % (count, total))

        if count % 10 == 0:
            session.commit()

    session.commit()
示例#41
0
def log(user, message):
    if not LOGGING_ENABLED:
        return
    session = Session()
    message = AuditLogMessage(
            user=user, message=message, player_name=PLAYER_NAME)
    session.add(message)
    session.commit()
示例#42
0
文件: app.py 项目: VeftSkil3/Kodemon
def addPost():
    title = request.form.get('title')
    year = request.form.get('year')
    session = Session()
    m = Movie(title=title, year=year)
    session.add(m)
    session.commit()
    return redirect('/')
示例#43
0
 def on_post(self, req, resp):
     doc = req.context['doc']
     users=Session.query(User).all()
     unique=True
     for user in users:
         if doc['email'] == user.email:
             unique=False
     if unique:
         user = User(name=doc['name'], email=doc['email'].lower(), signedin=False,registerTime=datetime.datetime.today())
         print(datetime.datetime.today())
         user.salt = bcrypt.gensalt()
         user.pw_hash = bcrypt.hashpw(doc['password'].encode('utf-8'), user.salt)
         s=smtplib.SMTP_SSL('smtp.gmail.com',465)
         s.ehlo()
         s.login('*****@*****.**','helloworld@ppclub')
         code=randint(1000000,10000000)
         user.code=code
         msg=MIMEText('Hi '+user.name+', your verification URL is: '+'http://192.168.1.127:8000/confirmation/'+str(code))
         msg['From']='*****@*****.**'
         msg['To']=user.email
         msg['Subject']='PhoenixNow Account Confirmation'
         s.send_message(msg)
         s.close()
         Session.add(user)
         Session.flush()
         Session.commit()
         req.context['user'] = user.id
         req.context['result'] = {"result": "success", "action": "register"}
     else:
         user=get_user(req,resp)
         td=datetime.timedelta(minutes=30)
         if datetime.datetime.today()-td<user.registerTime or user.emailVerified==True:
             description = "User was already made"
             title = "User creation conflict"
             raise falcon.HTTPConflict(title=title, description=description)
         else:
             Session.delete(user)
             Session.flush()
             user = User(name=doc['name'], email=doc['email'], signedin=False,registerTime=datetime.datetime.today())
             print(datetime.datetime.today())
             user.salt = bcrypt.gensalt()
             user.pw_hash = bcrypt.hashpw(doc['password'].encode('utf-8'), user.salt)
             s=smtplib.SMTP_SSL('smtp.gmail.com',465)
             s.ehlo()
             s.login('*****@*****.**','helloworld@ppclub')
             code=randint(1000000,10000000)
             user.code=code
             msg=MIMEText('Hi '+user.name+', your verification URL is: '+'http://192.168.1.127:8000/confirmation/'+str(code))
             msg['From']='*****@*****.**'
             msg['To']=user.email
             msg['Subject']='PhoenixNow Account Confirmation'
             s.send_message(msg)
             s.close()
             Session.add(user)
             Session.flush()
             Session.commit()
             req.context['user'] = user.id
             req.context['result'] = {"result": "success", "action": "register"}
示例#44
0
def submit(uid, problem, lang, source, fn):
    #Validate parameters
    if not lang in ['cc','python','java']:
        if fn[-3:] == '.py': 
            lang = 'python'
        elif fn[-3:] in ['.cc','.c','.cpp','.cxx']:
            lang = 'cc'
        elif fn[-3:] in ['.java']:
            lang = 'java'
        else:
            p = re.compile(r'\$\$[ ]*language:[ ]*(python|cc|java)[ ]*\$\$')
            m = p.search(source)
            if m: 
                lang = m.group(1)
            else:
                #Todo detect by magic regexps
                return "Unable to detect language"
    if problem == -1:
        p = re.compile(r'\$\$[ ]*problem:[ ]*([0-9]+)[ ]*\$\$')
        m = p.search(source)
        if m:
            problem = int(m.group(1))
        else:
            p = re.compile(r'(0-9)+')
            m = p.search(fn)
            if m:
                problem = int(m.group(1))
            else:
                return "Unable to detect problem number"
    if len(source) < 5:
        return "No source code submitted"
    #Send job to overlord
    try:
        code, msg = client.judge(problem, lang, source)

        session = Session()
        
        s = Submission()
        s.submitter_id = uid
        s.source = source
        s.problem_id = problem
        s.submitTime = datetime.utcnow()
        if int(code) == 12:
            s.jobid = int(msg)
            s.code = -1
            s.msg = ''
        else:
            s.msg = msg
            s.code = code
            s.jobid = None
            s.judgeTime = datetime.utcnow()

        session.add(s)
        session.commit()
        
        return ""
    except Exception as e:
        return str(e)
示例#45
0
 def setUp(self):
     Base.metadata.drop_all(engine)
     Base.metadata.create_all(engine)
     p1 = Pyexcel(id=0, name="Adam", weight=11.25, birth=datetime.date(2014, 11, 11))
     session = Session()
     session.add(p1)
     p1 = Pyexcel(id=1, name="Smith", weight=12.25, birth=datetime.date(2014, 11, 12))
     session.add(p1)
     session.commit()
示例#46
0
def store(data):
    session = Session()
    if Data.compare_last(session, data):
        item = Data(**data)
        session.add(item)
        session.commit()
        logging.info('Stored!')
    else:
        logging.debug('No Changes')
示例#47
0
def add_song_to_playlist(playlist_id, song_id):
    session = Session()
    append_index = session.query(PlaylistItem) \
            .filter_by(playlist_id=playlist_id) \
            .count()
    item = PlaylistItem(playlist_id=playlist_id, song_id=song_id, index=append_index)
    session.add(item)
    session.commit()
    return get_playlist(playlist_id)
示例#48
0
 def post(self):
     session = Session()
     
     new_user = db.User(flask.request.form['username'], flask.request.form['first_name'], flask.request.form['last_name'], flask.request.form['password'])
     session.add(new_user)
     session.commit()
     flask.flash('Welcome! Please log in now!')
     
     return flask.redirect(flask.url_for('login'))
示例#49
0
 def update_tags(cls, xg_device_token, tags):
     try:
         r = Session.query(cls).filter(cls.xg_device_token == xg_device_token).first()
         r.tags = tags
         Session.add(r)
         Session.commit()
     except sqlalchemy.exc.IntegrityError, e:
         logger.warning('msg[update tags error] table[%s] e[%s]' % (__tablename__, e))
         Session.rollback()
示例#50
0
 def addRelation(self, user, event):
     s = Session()
     attendee = Attendee(user,event)
     try:
         s.add(attendee)
         s.commit()
     except:
         return False
     finally:
         s.close()
     return True
示例#51
0
def add():
    if request.method == 'GET':
        return render_template('add-movie.html')
    else:
        title = request.form.get('title')
        year = request.form.get('year')
        session = Session()
        m = Movie(title=title, year=year)
        session.add(m)
        session.commit()
        return redirect('/')
示例#52
0
def createOrganization(json1):
    e = Organization.fromdict(json1)
    s = Session()
    try:
        s.add(e)
        s.commit()
    except:
        print("here")
        return False
    finally:
        s.close()
    return True
 def test_07_interests_write(self):
     session = Session()
     joey = session.query(Volunteer).filter_by(name='Joey Wood').first()
     moey = VolunteerInterests("youth", joey.id)
     self.assertTrue(moey.interest == "youth")
     try:
         session.add(moey)
         session.commit()
         session.close()
         self.assertTrue(True)
     except exc.SQLAlchemyError:
         self.assertTrue(False)
 def test_09_neighborhood_write(self):
     session = Session()
     joey = session.query(Volunteer).filter_by(name='Joey Wood').first()
     moey = VolunteerNeighborhoods("backbay", joey.id)
     self.assertTrue(moey.neighborhood == "backbay")
     try:
         session.add(moey)
         session.commit()
         session.close()
         self.assertTrue(True)
     except exc.SQLAlchemyError:
         self.assertTrue(False)
 def test_11_skill_write(self):
     session = Session()
     joey = session.query(Volunteer).filter_by(name='Joey Wood').first()
     moey = VolunteerSkills(SkillsEnum.teaching, joey.id)
     self.assertTrue(moey.skill == "teaching")
     try:
         session.add(moey)
         session.commit()
         session.close()
         self.assertTrue(True)
     except exc.SQLAlchemyError:
         self.assertTrue(False)
示例#56
0
 def test_11_interests_write(self):
     session = Session()
     race = session.query(Event).filter_by(name='Race for the Cure').first()
     dace = EventInterests("Youth", race.id)
     self.assertTrue(dace.interest == "Youth")
     try:
         session.add(dace)
         session.commit()
         session.close()
         self.assertTrue(True)
     except exc.SQLAlchemyError:
         self.assertTrue(False)
示例#57
0
 def test_13_neighborhood_write(self):
     session = Session()
     race = session.query(Event).filter_by(name='Race for the Cure').first()
     dace = EventNeighborhoods("Back Bay", race.id)
     self.assertTrue(dace.neighborhood == "Back Bay")
     try:
         session.add(dace)
         session.commit()
         session.close()
         self.assertTrue(True)
     except exc.SQLAlchemyError:
         self.assertTrue(False)
示例#58
0
 def test_15_skill_write(self):
     session = Session()
     race = session.query(Event).filter_by(name='Race for the Cure').first()
     dace = EventSkills("Teaching/Tutoring", race.id)
     self.assertTrue(dace.skill == "Teaching/Tutoring")
     try:
         session.add(dace)
         session.commit()
         session.close()
         self.assertTrue(True)
     except exc.SQLAlchemyError:
         self.assertTrue(False)
    def _update_task_list(self, limit=10, st=0, ignore=False):
        now = self.time()
        with self._last_update_task_lock:
            if now <= self._last_update_task and limit <= self._last_update_task_size:
                return
            self._last_update_task = self.time()
            self._last_update_task_size = limit

            session = Session()
            tasks = self.xunlei.get_task_list(limit, st)
            for task in tasks[::-1]:
                db_task_status = session.query(db.Task.status).filter(
                        db.Task.id == task['task_id']).first()
                if db_task_status and db_task_status[0] == "finished":
                    continue

                db_task = self.get_task(int(task['task_id']))
                changed = False
                if not db_task:
                    changed = True
                    db_task = db.Task()
                    db_task.id = task['task_id']
                    db_task.create_uid = self.uid
                    db_task.cid = task['cid']
                    db_task.url = task['url']
                    db_task.lixian_url = task['lixian_url']
                    db_task.taskname = task['taskname'] or "NULL"
                    db_task.task_type = task['task_type']
                    db_task.status = task['status']
                    db_task.invalid = True
                    db_task.process = task['process']
                    db_task.size = task['size']
                    db_task.format = task['format']
                else:
                    db_task.lixian_url = task['lixian_url']
                    if db_task.status != task['status']:
                        changed = True
                        db_task.status = task['status']
                    if db_task.status == "failed":
                        db_task.invalid = True
                    if db_task.process != task['process']:
                        changed = True
                        db_task.process = task['process']

                session.add(db_task)
                if changed and not self._update_file_list(db_task, session):
                    db_task.status = "failed"
                    db_task.invalid = True
                    session.add(db_task)
                
            session.commit()
            session.close()