Exemple #1
0
def create_db():
    FLASK_CONFIG = os.getenv("FLASK_CONFIG", "None")
    app = create_app(FLASK_CONFIG)
    app_context = app.app_context()
    app_context.push()

    from project.serate.models import Serata
    from project.corsi.models import Corso
    from project.tags.models import Tag
    from project.ruoli.models import Ruolo

    try:
        ruolo_list = Ruolo.query.all()

    except Exception as message:
        print("No db data")
        print("Creazione struttura senza utenti, post e commenti")
        db.create_all()
        print("Start creating roles")
        Ruolo.insert_roles()
        print("Start creating tags")
        Tag.insert_test_tags()
        print("Start creating corsi")
        Corso.insert_test_corsi()
        print("Start creating serate")
        Serata.insert_test_serate()

    db.session.remove()
    app_context.pop()
 def setUp(self):
     self.app = create_app('testing')
     self.app_context = self.app.app_context()
     self.app_context.push()
     db.create_all()
     Tag.insert_test_tags()
     Corso.insert_test_corsi()
     Serata.insert_test_serate()
     self.client = self.app.test_client()
Exemple #3
0
def create_test_db():
    print("Start creating test db")
    FLASK_CONFIG = os.getenv("FLASK_CONFIG", "None")
    app = create_app(FLASK_CONFIG)
    app_context = app.app_context()
    app_context.push()

    from project.serate.models import Serata
    from project.corsi.models import Corso
    from project.tags.models import Tag
    from project.ruoli.models import Ruolo
    from project.utenti.models import Utente
    from project.blog.models import Post
    from project.commenti.models import Comment

    from random import randint
    from faker import Faker

    try:
        user_list = Utente.query.all()
        course_list = Corso.query.all()
        post_list = Post.query.all()
        comment_list = Comment.query.all()
        post_list = Post.query.all()
        ruolo_list = Ruolo.query.all()
        print("DB Tables already exists")

    except Exception as message:
        print(f"No db data exist, inserting them:")

        def users(count=100):
            fake = Faker("it_IT")
            i = 0
            while i < count:
                u = Utente(
                    email=fake.email(),
                    username=fake.user_name(),
                    password="******",
                    confirmed=True,
                    name=fake.name(),
                    location=fake.city(),
                    about_me=fake.text(),
                    member_since=fake.past_date(),
                )
                db.session.add(u)
                try:
                    db.session.commit()
                    i += 1
                except IntegrityError:
                    db.session.rollback()

        def posts(count=100):
            fake = Faker("it_IT")
            user_count = Utente.query.count()
            for i in range(count):
                u = Utente.query.offset(randint(0, user_count - 1)).first()
                p = Post(body=fake.text(),
                         timestamp=fake.past_date(),
                         author=u)
                db.session.add(p)
                db.session.commit()

        def comments(count=100):
            fake = Faker("it_IT")
            user_count = Utente.query.count()
            post_count = Post.query.count()
            for i in range(count):
                u = Utente.query.offset(randint(0, user_count - 1)).first()
                p = Post.query.offset(randint(0, post_count - 1)).first()
                c = Comment(body=fake.text(),
                            timestamp=fake.past_date(),
                            post=p,
                            author=u)
                db.session.add(c)
                db.session.commit()

        print("Creating structure")
        db.create_all()
        db.session.commit()

        print("Creating roles")
        Ruolo.insert_roles()

        print("Creating fake users")
        users(2)

        print("Creating test users")
        Utente.insert_test_users()

        print("Creating tags")
        Tag.insert_test_tags()

        print("Creating corsi")
        Corso.insert_test_corsi()

        print("Creating serate")
        Serata.insert_test_serate()

        print("Creating posts fake")
        posts(3)

        print("Creating commenti fake")
        comments(3)

        print("\nDB Dummy data inserted succesfully")
    db.session.remove()
    app_context.pop()
    print("Creating structure")    
    db.create_all()

    print("Creating roles")
    Ruolo.insert_roles()
    
    print("Creating fake users")
    users(20)
    print("Creating test users")
    Utente.insert_test_users()

    print("Creating tags")
    Tag.insert_test_tags()

    print("Creating corsi")
    Corso.insert_test_corsi()
    
    print("Creating serate")
    Serata.insert_test_serate()

    print("Creating posts fake")
    posts()

    print("Creating commenti fake")    
    comments()

###DEBUGS
print("\n#### DATA DEBUG ####\n")

# Read a course
courses = Corso.query.all()