Beispiel #1
0
    def post(self):
        """
        headers = Token
        args = {"title": title, "text": text, "tags": tag1,tag2,tag3,...}
        """

        user = auth_token()
        if not user:
            return response_model(), 401

        req = get_json_data("title", "text", "tags")
        if not req:
            return response_model(), 400

        # author -> this is user from authentication

        # add Post object
        with db.transaction():
            try:
                p = Post(author=user, title=req['title'].encode('utf-8'), text=req['text'].encode('utf-8'))
                p.save()

                for tag in req['tags'].split(","):
                    tmp, _ = Tag.get_or_create(name=tag)
                    apat = AssociationPostAndTag(post=p, tag=tmp)
                    apat.save()
            except:
                db.rollback()
                return response_model(), 400


        return response_model(), 200
Beispiel #2
0
def add_people():

    FIRST_NAME = 0
    LAST_NAME = 1

    donors = [('Jim', 'Halpert'), ('Pam', 'Beesley'), ('Dwight', 'Shrute'),
              ('Michael', 'Scott'), ('Andy', 'Bernard')]

    try:
        db.connect()
        for donor in donors:
            code = base64.b32encode(os.urandom(8)).decode().strip('=')
            with db.transaction():
                n = Donor.create(  # noqa F403
                    code=code,
                    first_name=donor[FIRST_NAME],
                    last_name=donor[LAST_NAME])
                n.save()

        logger.info('People added.')

    except Exception as e:
        logger.info(e)

    finally:
        logger.info('database closes')
        db.close()
Beispiel #3
0
def add_donations():

    DONATION_AMT = 0
    LAST_NAME = 1

    donations = [(1.0, 'Halpert'), (1000.0, 'Beesley'), (2000.0, 'Beesley'),
                 (3000.0, 'Beesley'), (2.0, 'Shrute'), (3.0, 'Shrute'),
                 (10.0, 'Scott'), (20.0, 'Scott'), (30.0, 'Scott'),
                 (10.0, 'Bernard'), (20.0, 'Bernard'), (30.0, 'Bernard')]

    try:
        db.connect()
        for donation in donations:
            with db.transaction():
                n = Donation.create(  # noqa F403
                    donation=donation[DONATION_AMT],
                    donor=donation[LAST_NAME])
                n.save()

        logger.info('Donations added.')

    except Exception as e:
        logger.info(e)

    finally:
        logger.info('database closes')
        db.close()
Beispiel #4
0
    def confirm(self, user, message_text):
        values = re.split(CONFIRM_REGEX, message_text)

        #0: blank, 1: match_id, 2: blank
        if not values or len(values) != 3:
            return

        try:
            #http://stackoverflow.com/questions/24977236/saving-peewee-queries-with-multiple-foreign-key-relationships-against-the-same-t
            Winner = Player.alias()
            Loser = Player.alias()
            match = Match.select(Match, Winner, Loser).join(
                Winner, on=(Match.winner == Winner.slack_id)).join(
                    Loser, on=(Match.loser == Loser.slack_id)).where(
                        Match.id == values[1], Match.loser == user,
                        Match.pending == True).get()

            with db.transaction():
                match.winner.wins += 1
                match.loser.losses += 1

                winner_old_elo = match.winner.rating
                loser_old_elo = match.loser.rating

                #https://metinmediamath.wordpress.com/2013/11/27/how-to-calculate-the-elo-rating-including-example/
                winner_transformed_rating = 10**(match.winner.rating / 400.0)
                loser_transformed_rating = 10**(match.loser.rating / 400.0)

                winner_expected_score = winner_transformed_rating / (
                    winner_transformed_rating + loser_transformed_rating)
                loser_expected_score = loser_transformed_rating / (
                    winner_transformed_rating + loser_transformed_rating)

                match.winner.rating = round(match.winner.rating +
                                            match.winner.k_factor() *
                                            (1 - winner_expected_score))
                match.loser.rating = round(match.loser.rating +
                                           match.loser.k_factor() *
                                           (0 - loser_expected_score))

                match.pending = False
                match.save()
                match.winner.save()
                match.loser.save()

                self.talk('<@' + match.winner.slack_id +
                          '> your new ELO is: ' + str(match.winner.rating) +
                          ' You won ' +
                          str(match.winner.rating - winner_old_elo) + ' ELO')
                self.talk('<@' + match.loser.slack_id + '> your new ELO is: ' +
                          str(match.loser.rating) + ' You lost ' +
                          str(abs(match.loser.rating - loser_old_elo)) +
                          ' ELO')
        except Exception as e:
            self.talk('Unable to confirm ' + values[1] + '. ' + str(e))
Beispiel #5
0
def user_add():
    data = load_request_data(request)
    errors = user_data_validator.validate(data)
    if not errors:
        data = dict(data)
        data['password'] = User.gen_password(data['password'])
        with db.transaction():
            User.create(**data)
        response = HTTPResponse({}, status=201)
    else:
        response = HTTPResponse({'errors': {'fields': errors}}, status=400)
    return response
Beispiel #6
0
async def main():
    await init_connections()
    allow_labels = {'car', 'truck', 'cell_phone', 'bus'}
    detector_id = random.randint(1, 2**24)
    detector = Detector()
    logging.basicConfig(
        level=logging.ERROR,
        filename='%s::%d.log' % (__main__.__file__[:-3], detector_id),
        format=
        u'%(filename)s[LINE:%(lineno)d] #%(levelname)s [%(asctime)s] %(message)s'
    )

    while True:
        try:
            camera_id = await get_camera_id(detector_id=detector_id)
            if camera_id is None:
                await asyncio.sleep(args.delay)
                continue
            places = await get_places(camera_id=camera_id)
            await download_frame(camera_id=camera_id)
            objects = detector(image=args.filename)
            dist = [((obj.x - place.x)**2 + (obj.y - place.y)**2, obj, place)
                    for obj in objects if obj.label in allow_labels
                    for place in places]
            dist.sort(key=lambda x: x[0])
            used = set()
            values = dict()
            for place in places:
                values[place.id] = False
            for d, obj, place in dist:
                if (obj.x, obj.y) in used:
                    continue
                if (obj.x - obj.width // 2) <= place.x <= (obj.x + obj.width // 2) and \
                        (obj.y - obj.height // 2) <= place.y <= (obj.y + obj.height // 2):
                    used.add((obj.x, obj.y))
                    values[place.id] = True
            async with db.transaction() as tx:
                for place in places:
                    if place.status != values[place.id]:
                        await Place.update \
                            .values(status=values[place.id]) \
                            .where(Place.id == place.id) \
                            .gino \
                            .status()
                await Camera.update\
                    .values(is_complete=True, detector_id=0, last_update=int(time.time()))\
                    .where(Camera.id == camera_id)\
                    .gino\
                    .status()
        except Exception as msg:
            logging.error(str(msg))
Beispiel #7
0
def reg():
    form = EmailUsernameForm()
    if form.validate_on_submit():
        email = form.email.data
        username = form.username.data
        try:
            with db.transaction():
                user = User.create(email=email, username=username, join_date=datetime.datetime.now())
            log_user(user)
            return redirect(url_for('index'))
        except:
            return render_template('register.html', form=form, existed_error=True)
            #print ('failed.')
    return render_template('register.html', form=form, existed_error=False)
Beispiel #8
0
def conv_info_models(model, mongo_objects):
    print('Start converting {}'.format(str(model)))

    # Create mysql objects
    with db.transaction():
        for mongo_object in mongo_objects:
            model.create(
                name=mongo_object['name'].strip(),
                created_at=mongo_object['date_created'])

    # Make a map of mongo_id to mysql_id
    mongo_id_to_mysql_id = {str(mongo_object['_id']): i + 1 for i, mongo_object in enumerate(mongo_objects)}

    print('End converting {}'.format(str(model)))
    return mongo_id_to_mysql_id
Beispiel #9
0
def main():
    # Parse command line
    import argparse
    parser = argparse.ArgumentParser(description='Add words to the SketchWithUs database')
    parser.add_argument('input', help='file containing one word per line')
    args = parser.parse_args()

    # Connect to the database
    db.connect()

    # Insert all the words
    with db.transaction():
        for chunk in _grouper(1000, _file_iter(args.input)):
            print chunk
            Word.insert_many({'text': x, 'plays': 0, 'wins': 0} for x in chunk).execute()
Beispiel #10
0
def task_update(user, task_id):
    task = get_task_or_404(task_id, user)

    data = load_request_data(request)
    errors = task_data_validator.validate(data, ignore_field_names=('owner', ), create=False)

    if not errors:
        data = dict(data)
        with db.transaction():
            Task.update(**data).where(Task.id == task_id).execute()
        response = HTTPResponse({}, status=200)
    else:
        data = {'errors': {'fields': errors}}
        response = HTTPResponse(data, status=400)

    return response
Beispiel #11
0
    def apply_match(self, match):
        """
        Apply a match to the ranking system, changing winner and loser's elos.
        Return (winner elo delta, loser elo delta)
        """
        if not match.pending:
            raise ValuError("Match must be pending to apply.")

        with db.transaction():
            winner = self.players[match.winner_handle]
            loser = self.players[match.loser_handle]
            winner_elo_delta, loser_elo_delta = self.rank_game(winner, loser)

            match.pending = False
            match.save()

        return (winner_elo_delta, loser_elo_delta)
Beispiel #12
0
def load(fp):
  reader = csv.reader(fp)
  reader.next() # Throw out the column headers

  with db.transaction():
    for row in reader:
      crime = Crime(
        district = row[0],
        sector = row[1],
        dispatch_time = row[2],
        location = row[7],
        code = row[8],
        description = row[10],
        lon = row[12],
        lat = row[13])
      crime.save(force_insert = True)

  db.execute_sql("""
    UPDATE crime SET geom = ST_GeomFromText(
      'POINT(' || lon || ' ' || lat || ')', 4326
    );""")
Beispiel #13
0
    def confirm(self, user, message_text):
        values = re.split(CONFIRM_REGEX, message_text)

        #0: blank, 1: match_id, 2: blank
        if not values or len(values) != 3:
            return

        try:
            #http://stackoverflow.com/questions/24977236/saving-peewee-queries-with-multiple-foreign-key-relationships-against-the-same-t
            Winner = Player.alias()
            Loser  = Player.alias()
            match = Match.select(Match, Winner, Loser).join(Winner, on=(Match.winner == Winner.slack_id)).join(Loser, on=(Match.loser == Loser.slack_id)).where(Match.id == values[1], Match.loser == user, Match.pending == True).get()

            with db.transaction():
                match.winner.wins  += 1
                match.loser.losses += 1

                winner_old_elo = match.winner.rating
                loser_old_elo  = match.loser.rating

                #https://metinmediamath.wordpress.com/2013/11/27/how-to-calculate-the-elo-rating-including-example/
                winner_transformed_rating = 10**(match.winner.rating/400.0)
                loser_transformed_rating  = 10**(match.loser.rating/400.0)

                winner_expected_score = winner_transformed_rating /(winner_transformed_rating + loser_transformed_rating)
                loser_expected_score  = loser_transformed_rating /(winner_transformed_rating + loser_transformed_rating)

                match.winner.rating = round(match.winner.rating + match.winner.k_factor() * (1 - winner_expected_score))
                match.loser.rating = round(match.loser.rating + match.loser.k_factor() * (0 - loser_expected_score))

                match.pending = False
                match.save()
                match.winner.save()
                match.loser.save()

                self.talk('<@' + match.winner.slack_id + '> your new ELO is: ' + str(match.winner.rating) + ' You won ' + str(match.winner.rating - winner_old_elo) + ' ELO')
                self.talk('<@' + match.loser.slack_id + '> your new ELO is: ' + str(match.loser.rating) + ' You lost ' + str(abs(match.loser.rating - loser_old_elo)) + ' ELO')
        except Exception as e:
            self.talk('Unable to confirm ' + values[1] + '. ' + str(e))
Beispiel #14
0
def conv_books(mongo_books, publisher_ids, author_ids):
    print('Start converting books')

    with db.transaction():
        for i, book in enumerate(mongo_books):
            Books.create(
                asin=book['asin'],
                publish_type=book['binding'] if 'binding' in book else None,
                date_publish='%d%02d%02d' % (book['date_publish'].year, book['date_publish'].month, book['date_publish'].day),
                image_s_url=book['images']['s']['url'],
                image_s_width=book['images']['s']['size']['width'],
                image_s_height=book['images']['s']['size']['height'],
                image_m_url=book['images']['m']['url'],
                image_m_width=book['images']['m']['size']['width'],
                image_m_height=book['images']['m']['size']['height'],
                image_l_url=book['images']['l']['url'],
                image_l_width=book['images']['l']['size']['width'],
                image_l_height=book['images']['l']['size']['height'],
                name=book['title'],
                region=book['region'],
                publisher_id=publisher_ids[str(book['publisher_id'])] if str(book['publisher_id']) in publisher_ids else None,
                author_id=author_ids[str(book['author_id'])] if str(book['author_id']) in author_ids else None,
                updated_at=book['date_last_modify'],
                created_at=book['date_created']
            )

            if i % 100 == 0:
                book_num = len(mongo_books)
                sys.stdout.write('\r{0} {1}%'.format(i + 1, int((i + 1) / book_num * 100)))
                sys.stdout.flush()

    # Make a map of mongo_id to mysql_id
    mongo_id_to_mysql_id = {str(mongo_object['_id']): i + 1 for i, mongo_object in enumerate(mongo_books)}

    print('')
    print('End converting books')

    return mongo_id_to_mysql_id
Beispiel #15
0
def create_titles(mongo_books, book_ids):
    print('Start creating titles')

    with db.transaction():
        title_id = 0
        for i, book in enumerate(mongo_books):
            if not 'tree_type' in book or book['tree_type'] == 'main':
                Titles.create(
                    created_at=datetime.datetime.now()
                )
                title_id += 1
                asins = [book['asin']]
                if 'sub_asins' in book and book['sub_asins'] and len(book['sub_asins']):
                    asins.extend(book['sub_asins'])
                update_book_title(asins, title_id)

            if i % 100 == 0:
                book_num = len(mongo_books)
                sys.stdout.write('\r{0} {1}%'.format(i + 1, int((i + 1) / book_num * 100)))
                sys.stdout.flush()

    print('')
    print('End creating titles')
Beispiel #16
0
def confirm_email():
  if not check_referrer():
    return redirect("/index.html")

  if not under_rate_limit(): 
    return json.dumps({
      "result" : "failed",
      "error" : "too many requests, try again later"
    }, 420)

  with db.transaction():
    try:
      params = {}
      form = request.form
      params['first'] = form.get('first')
      params['last'] = form.get('last')
      params['zip_code'] = form.get('zip_code')
      email = form.get('email')
      params['redacted'] = int(form.get('redacted', False))
      params['token'] = Signature.token_for_email(email)
      if not Signature.check_for_duplicates(email):
        return (json.dumps({
        "result" : "error",
        "exception": "Duplicate email entered" 
        }), 401)
      signature = Signature.create(**params)
      signature.save()
      signature.send_confirmation_email(email)
      signature.record_email_sent()
      return json.dumps({
        "result" : "success"
        })
    except IntegrityError as ie: # duplicate email. maybe query first instead of this?
      return(json.dumps({
        "result" : "error",
        "exception": str(ie) 
        }), 401)
Beispiel #17
0
def task_add(user):
    app = default_app()

    data = load_request_data(request)
    errors = task_data_validator.validate(data, ignore_field_names=('owner', ))

    if not errors:
        data = dict(request.forms)
        if data.get('deadline'):
            data['deadline'] = convert_str_to_date(data['deadline'])

        with db.transaction():
            task = Task.create(owner=user, **data)

        location = app.get_url('task_show', task_id=task.get_id())
        data = {'id': task.get_id(), 'location': location}

        response = HTTPResponse(data, status=201)

    else:
        data = {'errors': {'fields': errors}}
        response = HTTPResponse(data, status=400)

    return response
Beispiel #18
0
 def wrapper(*args, **kwargs):
     with db.transaction() as txn:
         test_func(*args, **kwargs)
         txn.rollback()
Beispiel #19
0
def picture_add(site, host, netloc, csrf, logged_in, user_id_logged_in):
    """
    Add picture post url
    """

    # POST parameters
    site_id = int(request.POST.get('site'))
    user_id = int(request.POST.get('user'))
    portfolio_id = int(request.POST.get('portfolio'))
    title = request.POST.get('title')
    description = request.POST.get('description')

    # Portfolio
    try:
        portfolio = Portfolio.get(Portfolio.id == portfolio_id,
                                  Portfolio.site == site)
    except Portfolio.DoesNotExist:
        return dict(status=False, info='Portfólio não encontrado')

    # Upload picture image
    start_index = 1
    try:
        pictures = site.pictures.order_by(Picture.id.desc())
        last_picture = pictures.get()
        start_index = last_picture.id + 1
    except Picture.DoesNotExist:
        pass
    img_path = site.get_image_path(IMAGE_PATH)
    img_url = '%s/%s/' % (host, IMAGE_DIR)
    upload_response = upload_new_image(request, 'picture', start_index,
                                       img_path, img_url, ORIG_SIZE, NORM_SIZE,
                                       THUMB_SIZE)
    if not upload_response['status']:
        return upload_response

    # Upload data
    original_image = upload_response['original_filename']
    normalized_image = upload_response['normalized_filename']
    thumbnail_image = upload_response['thumbnail_filename']
    original_url = upload_response['original_url']
    normalized_url = upload_response['normalized_url']
    thumbnail_url = upload_response['thumbnail_url']

    # Create Picture
    picture_created = None
    with db.transaction():
        try:
            picture_created = Picture.create(site=site,
                                             portfolio=portfolio,
                                             title=title,
                                             description=description,
                                             original_image=original_image,
                                             normalized_image=normalized_image,
                                             thumbnail_image=thumbnail_image)
        except IntegrityError as exp:
            if picture_created:
                picture_created.delete_instance(IMAGE_PATH)
            else:
                site_img_path = site.get_image_path(IMAGE_PATH)
                if original_image:
                    original_file = os.path.join(site_img_path, original_image)
                    if os.path.exists(original_file):
                        os.remove(original_file)
                if normalized_image:
                    normalized_file = os.path.join(site_img_path,
                                                   normalized_image)
                    if os.path.exists(normalized_file):
                        os.remove(normalized_file)
                if thumbnail_image:
                    thumbnail_file = os.path.join(site_img_path,
                                                  thumbnail_image)
                    if os.path.exists(thumbnail_file):
                        os.remove(thumbnail_file)
            # Return error
            return dict(status=False, info='%s' % exp)

    # Lista de pictures atualizada
    try:
        picture_list = template('pictures_admin_list.html',
                                site=site,
                                host=host,
                                csrf=csrf,
                                portfolio=portfolio,
                                img_url=img_url)
    except Exception as exp:
        return dict(status=False, info='%s' % exp)

    # Return OK
    return dict(status=True,
                info='Adicionada com sucesso',
                picture_id=picture_created.get_id(),
                original=original_url,
                normalized=normalized_url,
                thumbnail=thumbnail_url,
                picture_list=picture_list)
Beispiel #20
0
__author__ = 'João Neto'
"""
Dinamic data
"""
modules_list = None
templates_dict = None
"""
Create Main Site Data
"""
MAIN_USER = None
MAIN_SITE = None
try:
    MAIN_USER = User.get(User.email == MAIN_EMAIL)
    MAIN_SITE = Site.get(Site.user == MAIN_USER)
except (User.DoesNotExist, Site.DoesNotExist) as e:
    with db.transaction():
        try:
            if not MAIN_USER:
                MAIN_USER = User.create(
                    email=MAIN_EMAIL,
                    password=md5(MAIN_PASSWORD.encode('utf-8')).hexdigest(),
                    name=MAIN_NAME,
                    active=True)
            if not MAIN_SITE:
                MAIN_SITE = Site.create(user=MAIN_USER,
                                        site_email=MAIN_EMAIL,
                                        active=True)
        except IntegrityError as ex:
            print("Main Data Fail:", ex)

# Verify Main Site Data
Beispiel #21
0
 async def afin():
     async with db.transaction() as tx:
         for table in db.sorted_tables:
             await tx.connection.scalar(
                 text(f'TRUNCATE {table.name} CASCADE;'))
Beispiel #22
0
"""
Dinamic data
"""
modules_list = None
templates_dict = None

"""
Create Main Site Data
"""
MAIN_USER = None
MAIN_SITE = None
try:
    MAIN_USER = User.get(User.email == MAIN_EMAIL)
    MAIN_SITE = Site.get(Site.user == MAIN_USER)
except (User.DoesNotExist, Site.DoesNotExist) as e:
    with db.transaction():
        try:
            if not MAIN_USER:
                MAIN_USER = User.create(email=MAIN_EMAIL,
                                        password=md5(MAIN_PASSWORD.encode('utf-8')).hexdigest(),
                                        name=MAIN_NAME,
                                        active=True)
            if not MAIN_SITE:
                MAIN_SITE = Site.create(user=MAIN_USER,
                                        site_email=MAIN_EMAIL,
                                        active=True)
        except IntegrityError as ex:
            print("Main Data Fail:", ex)


# Verify Main Site Data
Beispiel #23
0
def get_signatures_redacted():
  with db.transaction():
    signatures = Signature.select().where((Signature.redacted == True) & ~(Signature.signed >> None))
    return json.dumps([{"firstLen": len(s.first), "lastLen": len(s.last), "zip_code": s.zip_code, "date":str(s.signed)} for s in signatures])
Beispiel #24
0
def picture_add(site, host, netloc, csrf, logged_in, user_id_logged_in):
    """
    Add picture post url
    """

    # POST parameters
    site_id = int(request.POST.get('site'))
    user_id = int(request.POST.get('user'))
    portfolio_id = int(request.POST.get('portfolio'))
    title = request.POST.get('title')
    description = request.POST.get('description')

    # Portfolio
    try:
        portfolio = Portfolio.get(Portfolio.id == portfolio_id, Portfolio.site == site)
    except Portfolio.DoesNotExist:
        return dict(status=False, info='Portfólio não encontrado')

    # Upload picture image
    start_index = 1
    try:
        pictures = site.pictures.order_by(Picture.id.desc())
        last_picture = pictures.get()
        start_index = last_picture.id + 1
    except Picture.DoesNotExist:
        pass
    img_path = site.get_image_path(IMAGE_PATH)
    img_url = '%s/%s/' % (host, IMAGE_DIR)
    upload_response = upload_new_image(request, 'picture', start_index, img_path, img_url, ORIG_SIZE, NORM_SIZE, THUMB_SIZE)
    if not upload_response['status']:
        return upload_response

    # Upload data
    original_image = upload_response['original_filename']
    normalized_image = upload_response['normalized_filename']
    thumbnail_image = upload_response['thumbnail_filename']
    original_url = upload_response['original_url']
    normalized_url = upload_response['normalized_url']
    thumbnail_url = upload_response['thumbnail_url']

    # Create Picture
    picture_created = None
    with db.transaction():
        try:
            picture_created = Picture.create(site=site,
                                             portfolio=portfolio,
                                             title=title,
                                             description=description,
                                             original_image=original_image,
                                             normalized_image=normalized_image,
                                             thumbnail_image=thumbnail_image)
        except IntegrityError as exp:
            if picture_created:
                picture_created.delete_instance(IMAGE_PATH)
            else:
                site_img_path = site.get_image_path(IMAGE_PATH)
                if original_image:
                    original_file = os.path.join(site_img_path, original_image)
                    if os.path.exists(original_file):
                        os.remove(original_file)
                if normalized_image:
                    normalized_file = os.path.join(site_img_path, normalized_image)
                    if os.path.exists(normalized_file):
                        os.remove(normalized_file)
                if thumbnail_image:
                    thumbnail_file = os.path.join(site_img_path, thumbnail_image)
                    if os.path.exists(thumbnail_file):
                        os.remove(thumbnail_file)
            # Return error
            return dict(status=False, info='%s' % exp)

    # Lista de pictures atualizada
    try:
        picture_list = template('pictures_admin_list.html', site=site, host=host, csrf=csrf,
                                portfolio=portfolio, img_url=img_url)
    except Exception as exp:
        return dict(status=False, info='%s' % exp)

    # Return OK
    return dict(status=True, info='Adicionada com sucesso', picture_id=picture_created.get_id(),
                original=original_url, normalized=normalized_url, thumbnail=thumbnail_url,
                picture_list=picture_list)