Exemple #1
0
    def setUp(self):
        db_path = os.path.dirname(__file__)
        db_path = os.path.join(db_path, self.db_name)
        try:
            os.remove(db_path)
        except OSError:
            pass
        db = self.db = orm.Database('sqlite', self.db_name, create_db=True)
        
        class Genre(db.Entity):
            name = orm.Required(str)
            artists = orm.Set('Artist')
        
        class Artist(db.Entity):
            name = orm.Required(str)
            age = orm.Optional(int)
            genres = orm.Set(Genre)

        
        db.generate_mapping(check_tables=True, create_tables=True)
    
        with orm.db_session:
            pop = Genre(name='pop')
            a = Artist(name='Sia', age=40, genres=[pop])
    
        pony.options.INNER_JOIN_SYNTAX = True
    
        import logging
        logging.getLogger().setLevel(logging.INFO)
        orm.sql_debug(True)
def checknewscomments(news):
    orm.sql_debug(False)

    if not news:
        with orm.db_session:
            first_newsitem = orm.select(orm.min(x.id) for x in NewsItem).first()
            last_newsitem = orm.select(orm.max(x.id) for x in NewsItem).first()
        newsitem_id = first_newsitem
    else:
        i = 0

    while True:
        with orm.db_session:
            if not news:
                newsitem = NewsItem.select(lambda x: x.id >= newsitem_id and x.id <= last_newsitem).first()
                if not newsitem:
                    break
            else:
                if i >= len(news):
                    break
                if news[i].isdigit():
                    newsitem = NewsItem.get(id=int(news[i]))
                else:
                    newsitem = NewsItem.get(name=news[i])
                i += 1
                if not newsitem:
                    print('News item {} not found'.format(news[i - 1]))
                    continue

            print('News item {} ({})'.format(newsitem.id, newsitem.name))
            comments_list = newsitem.bl.select_comments().order_by('c.date, c.id')
            check_comments_for(newsitem, comments_list)

            newsitem_id = newsitem.id + 1
Exemple #3
0
def setup_development_environment():
    """
    Setups a development environment by enabling SQL debug and generates some default objects

    :return:
    """
    import hashlib

    # Enable SQL debug
    sql_debug(True)

    # Create test user
    test_name = "test"
    test_salt = "salt"
    test_password = hashlib.sha256("{name}{salt}".format(name=test_name, salt=test_salt).encode("utf8")).hexdigest()
    account = Account(name=test_name,
                      password=test_password,
                      salt=test_salt)

    # Create test universe
    universe = Universe.create(name="Test universe", owner=account)
    place = Place(
        name="Void",
        description="You are in the void",
        universe=universe
    )
Exemple #4
0
def config_logging(app):
    # pony_orm debug 信息
    if app.config.get("SQL_DEBUG"):
        sql_debug(True)
    # 设置 debug_level
    if app.config.get("DEBUG_LEVEL"):
        level = getattr(logging, app.config.get("DEBUG_LEVEL"))
        logging.basicConfig(level=level)

    formatter = logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]')
    debug_log = os.path.join(app.root_path, app.config['DEBUG_LOG'])
    debug_file_handler = RotatingFileHandler(debug_log, maxBytes=100000,
                                             backupCount=10)
    debug_file_handler.setLevel(logging.DEBUG)
    debug_file_handler.setFormatter(formatter)
    app.logger.addHandler(debug_file_handler)

    error_log = os.path.join(app.root_path, app.config['ERROR_LOG'])
    error_file_handler = RotatingFileHandler(error_log, maxBytes=100000,
                                             backupCount=10)
    error_file_handler.setLevel(logging.ERROR)
    error_file_handler.setFormatter(formatter)
    app.logger.addHandler(error_file_handler)
Exemple #5
0
def status(fail_on_warn):
    from mini_fiction.utils import status as status_module
    orm.sql_debug(False)
    with db_session:
        fails, warns = status_module.print_all(current_app)
        if fails or fail_on_warn and warns:
            sys.exit(1)
Exemple #6
0
def bae_init():
    import bae
    orm.sql_debug(0)
    fn = "../log/start_stop.log"
    start = arrow.now()

    with open(fn, "a") as f:
        print("start", os.getpid(), start, sep="\t", file=f)

    def exit():
        stop = arrow.now()
        with open(fn, "a") as f:
            print("stop", os.getpid(), stop, stop - start, sep="\t", file=f)


    atexit.register(exit)

    #tornado.options.options.log_file_prefix = b'../log/_{}.log'.format(basename)  # should be str in py2, and .. is /home/bae/
    tornado.options.options.log_file_prefix = b'../log/my_app.log'
    try:
        tornado.log.enable_pretty_logging()
    except Exception:
        traceback.print_exc()

    db.bind("mysql", host="sqld.duapp.com", port=4050, user=AK, passwd=SK, db=MYSQL_NAME)
    db.generate_mapping(create_tables=True)
def checkstoryvoting(story_ids):
    orm.sql_debug(False)

    if not current_app.story_voting:
        print('Story voting is disabled.')
        return

    if not story_ids:
        with orm.db_session:
            first_story = orm.select(orm.min(x.id) for x in Story).first()
            last_story = orm.select(orm.max(x.id) for x in Story).first()
        story_id = first_story
    else:
        story_ids_queue = story_ids[::-1]  # reversed

    while True:
        with orm.db_session:
            if not story_ids:
                stories = Story.select(lambda x: x.id >= story_id and x.id <= last_story).order_by(Story.id)[:50]
                if not stories:
                    break
            else:
                if not story_ids_queue:
                    break
                stories = list(Story.select(lambda x: x.id in story_ids_queue[-50:]).order_by(Story.id))
                story_ids_queue = story_ids_queue[:-50]
                if not stories:
                    continue

            changed_stories = []

            for story in stories:
                print('Story {}:'.format(story.id), end=' ', flush=True)
                old_count = story.vote_total  # TODO: rename to story.vote_count
                old_value = story.vote_value
                old_extra = story.vote_extra

                current_app.story_voting.update_rating(story)

                new_count = story.vote_total
                new_value = story.vote_value
                new_extra = story.vote_extra

                print('{} -> {}'.format(old_value, new_value), end='', flush=True)

                if old_count != new_count or old_value != new_value or json.loads(old_extra) != json.loads(new_extra):
                    print(' (changed)')
                    changed_stories.append(story)
                else:
                    print('')

            print('Saving...', end=' ', flush=True)
            for story in changed_stories:
                story.bl.search_update(update_fields={'vote_total', 'vote_value'})
            orm.commit()
            print('Done.', flush=True)

            if not story_ids:
                story_id = stories[-1].id + 1
Exemple #8
0
def zip_dump(path, keep_broken):
    from mini_fiction.dumpload import zip_dump as cmd
    orm.sql_debug(False)
    path = os.path.abspath(path)

    tm = time.time()
    cmd(path, keep_broken=keep_broken)
    print('Done with {:.2f}s: {}'.format(time.time() - tm, path))
Exemple #9
0
def init_db():
    """
    Init search queries log db
    """
    orm.sql_debug(True)
    db.bind(application.config['DB_TYPE'], application.config['DB_STRING'], create_db=True)
    db.generate_mapping(create_tables=True)
    print("Database initialized")
Exemple #10
0
def zip_dump(path, keep_broken):
    from mini_fiction.dumpload import zip_dump as cmd
    orm.sql_debug(False)
    path = os.path.abspath(path)

    tm = time.time()
    cmd(path, keep_broken=keep_broken)
    print('Done with {:.2f}s: {}'.format(time.time() - tm, path))
Exemple #11
0
def create_database(db_name):
    os.remove(db_name)

    sql_debug(False)
    db.bind(provider='sqlite', filename=db_name, create_db=True)
    db.generate_mapping(create_tables=True)

    return db
Exemple #12
0
def handle_debug(args):
    '''
    :param args: the namespace obj from ``ArgumentParser``
    '''
    config.debug = args.debug
    level = logging.DEBUG if config.debug else logging.INFO

    logging.basicConfig(level=level)
    orm.sql_debug(config.debug)
Exemple #13
0
def main():

    bind_db('FanfictionDB.db')

    # turn on debug mode
    sql_debug(True)

    # map the models to the database
    # and create the tables, if they don't exist
    db.generate_mapping(create_tables=True)
Exemple #14
0
def dumpdb(dirpath, entities_list, gzip_compression, silent):
    from mini_fiction.dumpload import dumpdb_console as cmd
    orm.sql_debug(False)
    tm = time.time()
    cmd(
        dirpath,
        entities_list if entities_list and 'all' not in entities_list else [],
        gzip_compression,
        verbosity=1 if silent else 2,
    )
    print('Done in {}.'.format(timedelta_format(time.time() - tm)))
Exemple #15
0
def init_db():
    from .config import DEBUG
    from .models import db

    if DEBUG:
        sql_debug(True)
        db.bind('sqlite', 'database.sqlite')
        db.generate_mapping(create_tables=True)
    else:
        from .config import DB_PASS, DB_HOST, DB_USER, DB_NAME, DB_PORT
        db.bind('postgres', user=DB_USER, password=DB_PASS, host=DB_HOST, database=DB_NAME, port=DB_PORT)
        db.generate_mapping(create_tables=False)

    return db
Exemple #16
0
def config_app(app):
    app.config.from_object('kkblog.config.default_config')
    if 'KKBLOG_CONFIG' in os.environ:
        app.config.from_envvar('KKBLOG_CONFIG')
    app.config["ARTICLE_DEST"] = os.path.join(app.root_path, app.config["ARTICLE_DEST"])
    if app.config["DEBUG"]:
        sql_debug(True)
    if app.config["ALLOW_CORS"]:
        from flask.ext.cors import CORS
        CORS(app)
    # create data dir
    dir_data = os.path.join(app.root_path, "data")
    if not os.path.exists(dir_data):
        os.makedirs(dir_data)
Exemple #17
0
def start_db():
    db = orm.Database()

    try:
        db.bind(provider='sqlite', filename='database.sqlite')
    except OSError as ex:
        expected_path = str(ex).split(": ")[1].replace("'", "")
        file_name = re.split("/", expected_path)[-1].replace("'", "")
        open(f"{expected_path.replace(file_name, '')}/{file_name}",
             "a").close()
        db.bind(provider='sqlite', filename='database.sqlite')

    orm.sql_debug(True)

    return db
Exemple #18
0
def create_core(**kwargs):
    schema = kwargs['name']
    user = DB_USER if kwargs['user'] is None else kwargs['user']
    password = DB_PASS if kwargs['pass'] is None else kwargs['pass']

    x = Database()
    load_tables(x, schema)

    if DEBUG:
        sql_debug(True)
        x.bind('sqlite', '../database.sqlite')
    else:
        x.bind('postgres', user=user, password=password, host=DB_HOST, database=DB_NAME)

    x.generate_mapping(create_tables=True)
	def load(developdb):
		from pony.orm import (sql_debug, commit, db_session, flush)
		from tools import LoadConfig
		from os import path
		wk = path.dirname(path.dirname(path.realpath(__file__)))
		conf = LoadConfig(wk).conf()
		sql_debug(True)
		#db.bind('sqlite', 'telesalud.sqlite', create_db=True)
		loadconf = lambda: conf.developdb if developdb else conf.productiondb
		db.bind('postgres', **loadconf())
		db.generate_mapping(create_tables=True)
		with db_session:
			[Tipo(nombre=nm) for nm in [u'Embarazada',u'Contacto']]; flush()
			pr = Persona(telf='76180435',ci='5669297',nombres=u'Luis Eduardo',apellidos=u'Miranda Barja',sexo='m')
			Usuario(persona=pr, login=u'eduardo',passwd=u'Mast3R',rol=u'Administrador'); commit()
Exemple #20
0
def checkcounters(only_modified, dry_run):
    orm.sql_debug(False)

    verbosity = 1 if only_modified else 2

    check_tag_stories_count(verbosity=verbosity, dry_run=dry_run)
    if verbosity:
        print('', file=sys.stderr)
    check_story_chapters_and_views_count(verbosity=verbosity, dry_run=dry_run)
    if verbosity:
        print('', file=sys.stderr)
    check_chapter_views_count(verbosity=verbosity, dry_run=dry_run)

    if verbosity and dry_run:
        print('', file=sys.stderr)
        print('(DRY RUN)', file=sys.stderr)
def checkcounters(only_modified, dry_run):
    orm.sql_debug(False)

    verbosity = 1 if only_modified else 2

    check_tag_stories_count(verbosity=verbosity, dry_run=dry_run)
    if verbosity:
        print('', file=sys.stderr)
    check_story_chapters_and_views_count(verbosity=verbosity, dry_run=dry_run)
    if verbosity:
        print('', file=sys.stderr)
    check_chapter_views_count(verbosity=verbosity, dry_run=dry_run)

    if verbosity and dry_run:
        print('', file=sys.stderr)
        print('(DRY RUN)', file=sys.stderr)
Exemple #22
0
def generate_statistics(from_, to, tag, lines):
    orm.sql_debug(True)
    from_ = parse_date(from_) if from_ else min_date()
    to = parse_date(to) if to else max_date()

    lines_clause, lines_params = array_where_clause("be.name", lines)

    if tag:
        where_clause = "tag = (select id from Tag where name = $tag)"
    else:
        where_clause = "date <= datetime($to, '+1 second') AND date >= $f"
    sql = "SELECT bt.date, bt.tag, be.value, be.name FROM " + \
          "BloodTestEntry as be JOIN BloodTest_BloodTestEntry as bte " + \
          "on be.id = bte.bloodtestentry " + \
          "JOIN BloodTest bt on bt.id = bte.bloodtest " + \
          "WHERE (" + lines_clause + ") AND bt.id in " + \
          "(SELECT id from BloodTest WHERE " + where_clause + ") " + \
          "ORDER BY bt.date ASC "

    sel = db.select(sql, globals={'f': from_, 'to': to, 'tag': tag, **lines_params})
    sel = list(map(lambda x: (datetime.strptime(x[0], '%Y-%m-%d %X.%f'), x[1], x[2], x[3]), sel))

    sorted_dates = sorted(set(map(itemgetter(0), sel)))
    x = list(map(format_date, sorted_dates))
    y = []

    for ref_name in lines:
        ref_obj = select(x for x in ReferenceName if x.name == ref_name).first()
        values = list(map(lambda x: x[2], filter(lambda x: x[3] == ref_obj.id, sel)))
        data = {'name': ref_name, 'data': values}
        y.append(data)

    fill_rect = []
    default_tag = get_default_tag().id
    for k, g in itertools.groupby(sel, lambda x: x[1]):
        if k == default_tag:
            continue
        g = list(g)
        from_ = min(map(lambda x: x[0], g))
        to = max(map(lambda x: x[0], g))
        fill_rect.append({'from': format_date(from_), 'to': format_date(to)})

    return {
        'x': x,
        'y': y,
        'fill': fill_rect
    }
Exemple #23
0
    def __init__(self, db_file, create_new=False, debug_db=False):
        # Load db from file
        self.db = orm.Database("sqlite", db_file, create_db=create_new)

        # Model definitions:
        # Each model class hinerits from a particular db instance, therefore
        # the model classes defined in a kollektion are direcly linked to the
        # db file that describes that kollektion.

        # Tag:
        # The name of the tag is used to search for tags in the db, each tag
        # has a color. Tags are organized in a tree-like structure: each tag
        # can have a parent tag (but only one) and many children.
        # Tags loops are to be avoided. If an entry is tagged by a tag, it is
        # considered tagged by all parents of that tag.
        class Tag(self.db.Entity):
            name = orm.Required(str, unique=True)
            color = orm.Optional(float)
            parent = orm.Optional("Tag", reverse="children")
            children = orm.Set("Tag", reverse="parent")
            entries = orm.Set('Entry')

        # Entry:
        # The description is what is shown about an Entry when entries are listed
        # Each entry has a creation date and a last modified date (automatically
        # updated at each save) and a set of Tags.
        # The data json is a list of items, each item must have a dict entry
        # named 'class' that identifies the item type (e.g. file, link, note.. etc)
        # everythhing else is left free: this allows for modification of the item
        # types (e.g. adding a new class of items, or a new property for an item)
        # without the need of changing the database structure.
        class Entry(self.db.Entity):
            description = orm.Required(str)
            creationdate = orm.Required(datetime)
            lastmodified = orm.Required(datetime)
            tags = orm.Set(Tag)
            data = orm.Required(orm.Json)

        # Save the db-connected classes in the kollection instance
        self.Tag = Tag
        self.Entry = Entry

        # Generate mappings between the db and the models defined above
        orm.sql_debug(debug_db)
        self.db.generate_mapping(create_tables=create_new)
Exemple #24
0
def index():
    orm.sql_debug(True)
    since = select(e.timestamp for e in History_Entry).min()
    top_songs = select(
        (t.title, count(t.entries)) for t in Track).order_by(-2)[:10]
    #who the f**k wrote this, order_by(-2) means we order by count(t.entries) descending
    ta = select((a.name, count(t.entries)) for t in Track
                for a in t.artist).order_by(-2)[:10]
    total_tracks = select((count(t.entries), t.duration) for t in Track)[:]
    #cant sum(count(t.entries * t.duration))
    total_listened = 0
    for track in total_tracks:
        total_listened += track[0] * track[1]
    return render_template('stats.html',
                           top=top_songs,
                           top_artist=ta,
                           total=strftime('%-H hours %-M minutes %-S seconds',
                                          gmtime(total_listened)),
                           date=since)
Exemple #25
0
def checkstorycomments(story_ids):
    orm.sql_debug(False)

    if not story_ids:
        with orm.db_session:
            first_story = orm.select(orm.min(x.id) for x in Story).first()
            last_story = orm.select(orm.max(x.id) for x in Story).first()
        story_id = first_story
    else:
        i = 0

    while True:
        with orm.db_session:
            if not story_ids:
                story = Story.select(
                    lambda x: x.id >= story_id and x.id <= last_story).first()
                if not story:
                    break
            else:
                if i >= len(story_ids):
                    break
                story = Story.get(id=story_ids[i])
                i += 1
                if not story:
                    print('Story {} not found'.format(story_ids[i - 1]))
                    continue

            print('Story {}'.format(story.id))
            comments_list = story.bl.select_comments().order_by('c.date, c.id')

            # Проверка story_published
            pub = story.published
            for c in comments_list:
                if c.story_published != pub:
                    print(' -{}: pub {} -> {}'.format(c.id, c.story_published,
                                                      pub))
                    c.story_published = pub
                    c.flush()

            # Всё остальное здесь
            check_comments_for(story, comments_list)

            story_id = story.id + 1
Exemple #26
0
def checkstorylocalcomments(story_ids):
    orm.sql_debug(False)

    if not story_ids:
        with orm.db_session:
            first_local = orm.select(orm.min(x.id)
                                     for x in StoryLocalThread).first()
            last_local = orm.select(orm.max(x.id)
                                    for x in StoryLocalThread).first()
        local_id = first_local
    else:
        i = 0

    while True:
        with orm.db_session:
            if not story_ids:
                local = StoryLocalThread.select(
                    lambda x: x.id >= local_id and x.id <= last_local).first()
                if not local:
                    break
            else:
                if i >= len(story_ids):
                    break
                story = Story.get(id=story_ids[i])
                i += 1
                if not story:
                    print('Story {} not found'.format(story_ids[i - 1]))
                    continue
                if not story.local:
                    print('Story {} has no StoryLocalThread'.format(
                        story_ids[i - 1]))
                    continue
                local = story.local

            print('Story {} / StoryLocalThread {}'.format(
                local.story.id, local.id))
            comments_list = local.bl.select_comments().order_by('c.date, c.id')

            # Всё остальное здесь
            check_comments_for(local, comments_list)

            local_id = local.id + 1
def checkwordscount(story_ids):
    orm.sql_debug(False)

    if story_ids:
        for story_id in story_ids:
            with orm.db_session:
                story_check_words_count(int(story_id))
        return

    with orm.db_session:
        first_story = orm.select(orm.min(x.id) for x in Story).first()

    story_id = first_story
    while True:
        with orm.db_session:
            story = Story.select(lambda x: x.id >= story_id).first()
            if not story:
                break
            story_check_words_count(story)
            story_id = story.id + 1
def checkstorycomments(story_ids):
    orm.sql_debug(False)

    if not story_ids:
        with orm.db_session:
            first_story = orm.select(orm.min(x.id) for x in Story).first()
            last_story = orm.select(orm.max(x.id) for x in Story).first()
        story_id = first_story
    else:
        i = 0

    while True:
        with orm.db_session:
            if not story_ids:
                story = Story.select(lambda x: x.id >= story_id and x.id <= last_story).first()
                if not story:
                    break
            else:
                if i >= len(story_ids):
                    break
                story = Story.get(id=story_ids[i])
                i += 1
                if not story:
                    print('Story {} not found'.format(story_ids[i - 1]))
                    continue

            print('Story {}'.format(story.id))
            comments_list = story.bl.select_comments().order_by('c.date, c.id')

            # Проверка story_published
            pub = story.published
            for c in comments_list:
                if c.story_published != pub:
                    print(' -{}: pub {} -> {}'.format(c.id, c.story_published, pub))
                    c.story_published = pub
                    c.flush()

            # Всё остальное здесь
            check_comments_for(story, comments_list)

            story_id = story.id + 1
Exemple #29
0
def _migrate(db,
             cmd,
             name=None,
             start=None,
             end=None,
             verbose=False,
             custom=False,
             dry=False,
             empty=False,
             fake_initial=False):
    debug = os.environ.get('PONY_DEBUG')
    if debug:
        cmd_exitstack.enter_context(drop_into_debugger())
    if verbose:
        orm.sql_debug(True)

    graph = MigrationGraph()
    with cmd_exitstack:
        if cmd == 'make':
            return graph.make(db=db,
                              empty=empty,
                              custom=custom,
                              description=name)
        elif cmd == 'list':
            show_migrations(db=db)
        elif cmd == 'apply':
            start = find_migration(start) if start else None
            end = find_migration(end) if end else None
            graph.apply(db=db,
                        is_fake=fake_initial,
                        dry_run=dry,
                        name_start=start,
                        name_end=end)
        elif cmd == 'sql':
            name = find_migration(name)
            graph.apply(db=db, dry_run=True, name_exact=name)
        else:
            raise MigrationCommandError(
                '%s is not a valid migration subcommand' % cmd)
    def conn(self, env="test", debug=False):
        """
        Return prod or test DB connections
        """
        if env == "prod":
            url = urlparse(os.environ["DATABASE_URL"])
            self.bind(
                'postgres',
                user=url.username,
                password=url.password,
                host=url.hostname,
                database=url.path[1:],
            )
        else:
            self.bind(
                'sqlite',
                '../code_of_points.sqlite',
                create_db=True,
            )
            sql_debug(debug)

        self.generate_mapping(create_tables=True)
Exemple #31
0
def test_common_pony():
    db = orm.Database()

    class Account(db.Entity):
        name = orm.Required(str)
        msgn_id = orm.Required(int)
        auth_code = orm.Required(str)

    db.bind('sqlite', ':memory:', create_db=True)
    db.generate_mapping(create_tables=True)
    orm.sql_debug(True)

    @orm.db_session
    def test():
        Account(name="haje01", msgn_id=3498, auth_code="1234")
        a = Account(name="haje02", msgn_id=2345, auth_code="1234")
        assert len(orm.select(a for a in Account)) == 2
        assert len(orm.select(a for a in Account if a.msgn_id == 2345)) == 1
        a.delete()
        assert len(orm.select(a for a in Account)) == 1

    test()
Exemple #32
0
def init_db():
    global __is_bound

    if not __is_bound:

        from augur import settings

        if settings.main.project.debug:
            sql_debug(True)

        if settings.main.datastores.main.type == "sqlite":
            dbtype = settings.main.datastores.main.type
            dbtarget = settings.main.datastores.main.sqlite.path
            db.bind('sqlite', filename=dbtarget, create_db=True)
            __is_bound = True
        elif settings.main.datastores.main.type == "postgres":
            pg_settings = settings.main.datastores.main.postgres
            dbtype = settings.main.datastores.main.type
            dbtarget = pg_settings.host + "/" + pg_settings.dbname + ":" + pg_settings.port
            db.bind('postgres',
                    user=pg_settings.username,
                    password=pg_settings.password,
                    host=pg_settings.host,
                    database=pg_settings.dbname,
                    port=pg_settings.port)
            __is_bound = True
        else:
            raise ValueError("Invalid database type configured: %s" %
                             augur.settings.main.datastores.main.type)

        if __is_bound:
            print("Database Configuration:")
            print("Type: %s" % dbtype)
            print("Target: %s" % dbtarget)

            db.generate_mapping(create_tables=True)
        else:
            print("No valid database configuration found")
def createsuperuser(username, email, noinput):
    sql_debug(False)

    if not username and noinput:
        print('You must use --username with --noinput.')
        sys.exit(1)

    if not username:
        while True:
            username = input('Username: '******'This field cannot be blank.')

    if not email and not noinput:
        email = input('Email address: ')

    if noinput:
        password = ''
    else:
        while True:
            password = getpass('Password: '******'Password (again): ')
            if password == password2:
                break
            del password2
            print('Your passwords didn\'t match.')

    with db_session:
        Author.bl.create({
            'username': username,
            'email': email,
            'password': password,
            'is_staff': True,
            'is_superuser': True,
        })
    print('Superuser created successfully.')
def checkstorylocalcomments(story_ids):
    orm.sql_debug(False)

    if not story_ids:
        with orm.db_session:
            first_local = orm.select(orm.min(x.id) for x in StoryLocalThread).first()
            last_local = orm.select(orm.max(x.id) for x in StoryLocalThread).first()
        local_id = first_local
    else:
        i = 0

    while True:
        with orm.db_session:
            if not story_ids:
                local = StoryLocalThread.select(lambda x: x.id >= local_id and x.id <= last_local).first()
                if not local:
                    break
            else:
                if i >= len(story_ids):
                    break
                story = Story.get(id=story_ids[i])
                i += 1
                if not story:
                    print('Story {} not found'.format(story_ids[i - 1]))
                    continue
                if not story.local:
                    print('Story {} has no StoryLocalThread'.format(story_ids[i - 1]))
                    continue
                local = story.local

            print('Story {} / StoryLocalThread {}'.format(local.story.id, local.id))
            comments_list = local.bl.select_comments().order_by('c.date, c.id')

            # Всё остальное здесь
            check_comments_for(local, comments_list)

            local_id = local.id + 1
Exemple #35
0
def checknewscomments(news):
    orm.sql_debug(False)

    if not news:
        with orm.db_session:
            first_newsitem = orm.select(orm.min(x.id)
                                        for x in NewsItem).first()
            last_newsitem = orm.select(orm.max(x.id) for x in NewsItem).first()
        newsitem_id = first_newsitem
    else:
        i = 0

    while True:
        with orm.db_session:
            if not news:
                newsitem = NewsItem.select(lambda x: x.id >= newsitem_id and x.
                                           id <= last_newsitem).first()
                if not newsitem:
                    break
            else:
                if i >= len(news):
                    break
                if news[i].isdigit():
                    newsitem = NewsItem.get(id=int(news[i]))
                else:
                    newsitem = NewsItem.get(name=news[i])
                i += 1
                if not newsitem:
                    print('News item {} not found'.format(news[i - 1]))
                    continue

            print('News item {} ({})'.format(newsitem.id, newsitem.name))
            comments_list = newsitem.bl.select_comments().order_by(
                'c.date, c.id')
            check_comments_for(newsitem, comments_list)

            newsitem_id = newsitem.id + 1
Exemple #36
0
 def load(developdb):
     from pony.orm import (sql_debug, commit, db_session, flush)
     from tools import LoadConfig
     from os import path
     wk = path.dirname(path.dirname(path.realpath(__file__)))
     conf = LoadConfig(wk).conf()
     sql_debug(True)
     #db.bind('sqlite', 'telesalud.sqlite', create_db=True)
     loadconf = lambda: conf.developdb if developdb else conf.productiondb
     db.bind('postgres', **loadconf())
     db.generate_mapping(create_tables=True)
     with db_session:
         [Tipo(nombre=nm) for nm in [u'Embarazada', u'Contacto']]
         flush()
         pr = Persona(telf='76180435',
                      ci='5669297',
                      nombres=u'Luis Eduardo',
                      apellidos=u'Miranda Barja',
                      sexo='m')
         Usuario(persona=pr,
                 login=u'eduardo',
                 passwd=u'Mast3R',
                 rol=u'Administrador')
         commit()
Exemple #37
0
def main():
    config_path = Path(sys.argv[1]) if len(
        sys.argv) > 1 else Path.home() / '.config' / 'whereisit.toml'
    with open(config_path) as f:
        config = toml.load(f)

    import logging
    logging.getLogger('aiohttp.client').setLevel(logging.ERROR)

    db_path = Path.home() / '.local' / 'share' / config['database']['path']
    orm.sql_debug(config['database'].get('debug', False))
    database.bind("sqlite", str(db_path), create_db=True)
    database.generate_mapping(create_tables=True)

    with orm.db_session():
        orm.select(
            t for t in database.Tracking
            if t.id not in list(config['trackings'].keys())).delete(bulk=True)

    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    with contextlib.closing(asyncio.get_event_loop()) as loop:
        tracker = Tracker(loop=loop, db=database, config=config)
        loop.create_task(tracker.run())
        loop.run_forever()
Exemple #38
0
def main():
    args = get_parsed_args()
    config_logger(args.log_level)

    # DB
    models.init()
    sql_debug(args.sql)

    print(' [*] Waiting for logs. To exit press CTRL+C')

    def callback(ch, method, properties, body):
        logger.info(f'{method.routing_key}')
        msg_from_client = connection.decompress(body)
        verdict = get_verdict(msg_from_client['problem'],
                              msg_from_client['language'],
                              msg_from_client['code'])
        print(f'{verdict!r}')
        ch.basic_ack(delivery_tag=method.delivery_tag)
        msg_to_courier = {**msg_from_client, **{'verdict': verdict}}
        with connection.CourierConnection(args.host) as conn:
            conn.send(msg_to_courier)

    with connection.JudgeConnection(args.host, args.language) as conn:
        conn.consume(callback)
Exemple #39
0

class OrderItem(db.Entity):
    """Товар (одна позиция) в заказе"""
    order = Required("Order")
    product = Optional('Product')
    amount = Required(int)  # 1 единица товара


class Menu:
    """Меню"""


db.bind(provider='sqlite', filename='shop.sqlite', create_db=True)
db.generate_mapping(create_tables=True)
sql_debug(True)


@db_session
def add_product():
    product_1 = Product(title='M200 "Intervention"',
                        description='Make your self a long joy',
                        price=11850,
                        unit='1')
    product_2 = Product(title='Mossberg 500',
                        description='For close relationships',
                        price=370.34,
                        unit='1')
    product_3 = Product(title='Ammo .408 Cheyenne Tactical',
                        price=10,
                        unit='200')
Exemple #40
0
# -*- coding: utf-8 -*-

from pony import orm
from settings import DB, DBDEBUG

db = orm.Database("mysql", **DB)


class Person(db.Entity):
    _table_ = "person" # 注意是 _table_,而不是__table__
    
    name = orm.Required(unicode)
    age = orm.Required(int)
    motto = orm.Optional(unicode)
    cars = orm.Set("Car")


class Car(db.Entity):
    _table_ = "car"
    
    owner = orm.Required(Person)
    model = orm.Required(unicode) 
    

orm.sql_debug(DBDEBUG)
db.generate_mapping(create_tables=False)

__all__ = ["Person", "Car", "db"]
Exemple #41
0
def seed(silent):
    from mini_fiction import fixtures
    orm.sql_debug(False)
    fixtures.seed(verbosity=1 if silent else 2, only_create=True)
        elif category == "alumni":
            s = cls.get(name=name)
            results = []
        
            for p in s.students:
                data = {}
                data['name'] = p.name
                data['profil_pic'] = p.profil_pic
                data['promotion'] = p.promotion
                data['donated_to'] = p.donated_to
                results.append(data)
             
        return results

# turn on debug mode
pny.sql_debug(True)

# map the models to the database
# and create the tables, if they don't exist
database.generate_mapping(create_tables=True)


for p in conf.schools.itervalues():
    School.add(**p)

for p in conf.projects.itervalues():
    Project.add(**p)
    
for p in conf.members.itervalues():
    Member.add(**p)
Exemple #43
0
def initsphinx():
    if current_app.config.get('SPHINX_DISABLED'):
        print('Please set SPHINX_DISABLED = False before initsphinx.',
              file=sys.stderr)
        sys.exit(1)

    orm.sql_debug(False)
    sys.stderr.write(' Cleaning...')
    sys.stderr.flush()
    with current_app.sphinx as sphinx:
        sphinx.delete('stories', id__gte=0)
    with current_app.sphinx as sphinx:
        sphinx.delete('chapters', id__gte=0)
    with current_app.sphinx as sphinx:
        sphinx.flush('stories')
    with current_app.sphinx as sphinx:
        sphinx.flush('chapters')
    sys.stderr.write('\n')
    sys.stderr.flush()

    ok = 0
    pk = 0
    stories = None
    with db_session:
        count = Story.select().count()
    while True:
        with db_session:
            stories = tuple(
                Story.select(lambda x: x.id > pk).prefetch(
                    Story.contributors,
                    Story.characters,
                    # Story.categories,
                    # Story.classifications,
                    Story.tags,
                ).order_by(Story.id)[:100])
            if not stories:
                break

            Story.bl.add_stories_to_search(stories, with_chapters=False)
            pk = stories[-1].id
            ok += len(stories)
            sys.stderr.write(' [%.1f%%] %d/%d stories\r' %
                             (ok * 100 / count, ok, count))
            sys.stderr.flush()

    with current_app.sphinx as sphinx:
        sphinx.flush('stories')

    sys.stderr.write('\n')

    ok = 0
    pk = 0
    chapters = None
    with db_session:
        count = Chapter.select().count()
    while True:
        with db_session:
            chapters = tuple(
                Chapter.select(lambda x: x.id > pk).order_by(Chapter.id)[:20])
            if not chapters:
                break

            Chapter.bl.add_chapters_to_search(chapters,
                                              update_story_words=False)
            pk = chapters[-1].id
            ok += len(chapters)
            sys.stderr.write(' [%.1f%%] %d/%d chapters\r' %
                             (ok * 100 / count, ok, count))
            sys.stderr.flush()

    with current_app.sphinx as sphinx:
        sphinx.flush('chapters')

    sys.stderr.write('\n')
    sys.stderr.flush()
Exemple #44
0
def _bind_db(db="../../welikesports.sqlite", debug=True):
    if debug:
        sql_debug(True)
    database.bind("sqlite", db, create_db=True)
    database.generate_mapping(create_tables=True)
Exemple #45
0
class Us(DB.Entity):
    name = P.Required(str)
    lastname = P.Required(str)
    uname = P.PrimaryKey(str, 32)  # id
    email = P.Required(str, 60, unique=True)
    pwd = P.Required(str)
    books = P.Set('Book')


class Book(DB.Entity):
    book_id = P.PrimaryKey(str)
    users = P.Set(Us)


P.sql_debug(True)
DB.generate_mapping(create_tables=True)


class Server():
    """docstring for Server"""
    def __init__(self):
        pass

    @P.db_session
    def add_us(self, **kw):
        try:
            new = Us(**kw)
            P.flush()
            return new
        except Exception as e:
Exemple #46
0
 def load_db(cls):
     sql_debug(setting.SQL_DEBUG)
     cls.db.generate_mapping(create_tables=True)
     return cls.db
def create_db():
    #打开调试模式
    pny.sql_debug(True)
    #映射模型数据库
    #如果它们不存在,则创建表
    database.generate_mapping(create_tables=True)
from flask import Flask
from flask.ext.restful import Api
from pony import orm
from flask.ext.cors import CORS

app = Flask(__name__)
CORS(app)

api = Api(app)
db = orm.Database()
db.bind('sqlite', 'test_db.sqlite',create_db=True)
from app.models.user import User
orm.sql_debug(True)
db.generate_mapping(create_tables=True)


from app.views import app
Exemple #49
0
import sys
from datetime import (date, datetime)
from pony.orm import (Database, Required, Optional, Set, db_session, sql_debug, show, select, commit)

db = Database()

sql_debug(True)

class Task(db.Entity):
	
	task = Required(str)
	date_created = Required(date)
	date_of_completion = Optional(date)

db.bind('sqlite', filename='Jara.sqlite',create_db=True)
db.generate_mapping(create_tables=True)


def main():
    
    actions = {
    '1': action_find_all,
    '2': action_find_by_pk,
    '3': action_find_by_created,
    '4': action_find_by_date_of_completion,
    '5': action_add_task,
    '6': action_update_task,
    '7': action_add_date_of_completion,
    '8': action_add_date_of_completion,
    'm': action_show_menu,
    'q': action_exit
Exemple #50
0
def init_db():
    orm.sql_debug(True)
    db.bind(app.config['DB_TYPE'], app.config['DB_STRING'], create_db=True)
    db.generate_mapping(create_tables=True)
    print("Database initialized")
Exemple #51
0
# coding: utf-8
'''
description:使用前将model加载,并生成map
Created by weibaohui on 14-5-14.

'''
from pony.orm import sql_debug
from autodb.Logic.Mysqldb import db
from autodb.Logic.Oracledb import db as odb
#注册model

import portal, sqllist, sqlresult,OracleUser,GUWANG,yunwei


#开启调试模式
sql_debug(False)
db.generate_mapping()
odb.generate_mapping()
Exemple #52
0
from pony import orm as manejador

manejador.sql_debug(True)
db = manejador.Database()
db2 = manejador.Database()

db.bind('postgres', user="******", password='******', host='localhost', database='sbcdata')
db2.bind('postgres', user="******", password='******', host='localhost', database='sbc')

class Data(db.Entity):
    id = manejador.PrimaryKey(int, auto=True)
    sujeto = manejador.Required(str)
    predicado = manejador.Required(str)
    objeto = manejador.Required(str)
db.generate_mapping(check_tables=True, create_tables=True)

class Info(db2.Entity):
    id = manejador.PrimaryKey(int, auto=True)
    sujeto = manejador.Required(str)
    predicado = manejador.Required(str)
    objeto = manejador.Required(str)
    reconciliation = manejador.Optional(str)

db2.generate_mapping(check_tables=True, create_tables=True)
Exemple #53
0
def loaddb(pathlist, silent, only_create):
    from mini_fiction.dumpload import loaddb_console as cmd
    orm.sql_debug(False)
    tm = time.time()
    cmd(pathlist, verbosity=1 if silent else 2, only_create=only_create)
    print('Done in {}.'.format(timedelta_format(time.time() - tm)))
def _bind_db(db="../../welikesports.sqlite", debug=True):
    if debug:
        sql_debug(True)
    database.bind("sqlite", db, create_db=True)
    database.generate_mapping(create_tables=True)
Exemple #55
0
import os
from pony import orm

import dbpony.pony_models as models

db_path = r'C:\Users\pawel\OneDrive\Dokumenty\python_Projects\scan_hvac_workbooks\example_data'
db_name = 'db.sqlite'
db_path = os.path.join(db_path, db_name)

models.db.bind('sqlite', db_path, create_db=True)
orm.sql_debug(False)
models.db.generate_mapping(create_tables=True)
Exemple #56
0
from decimal import Decimal
from pony.orm import Database, PrimaryKey, Required, Optional, sql_debug

from .conf import db_settings


db = Database(db_settings['db_engine'], db_settings['db_location'], create_db=True)


class ZipCodeModel(db.Entity):
    zip_number = PrimaryKey(unicode, 10)
    city = Required(unicode)
    state = Required(unicode)
    country = Required(unicode)
    latitude = Optional(float)
    longitude = Optional(float)
    timezone = Optional(unicode)


sql_debug(db_settings['debug'])
db.generate_mapping(create_tables=True)