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()
def create(): form = CorsiForm() if form.validate_on_submit(): name = form.name.data teacher = form.teacher.data level = form.level.data stato_corso = form.stato_corso.data description = form.description.data link_materiale = form.link_materiale.data n_course = Corso(name, teacher, level, description, "", stato_corso, link_materiale) db.session.add(n_course) form.name.data = "" form.description.data = "" form.teacher.data = "" form.level.data = "" form.stato_corso.data = "" form.link_materiale = "" try: db.session.commit() flash('Corso creato correttamente', 'success') return redirect(url_for('corsi.lista')) except Exception as e: db.session.rollback() flash("Errore durante la creazione del corso: %s" % str(e), 'danger') return render_template("corsi_create.html", form=form)
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()
for t in tags: try: db.session.add(t) db.session.commit() except Exception as e: print(f"Eccezione: {e}") db.session.rollback() ################################################# ##### 2. Create courses ###### # Relazione n:n TAG - CORSO print("Start creating corsi") corsoFlask = Corso( "Flask", "Andrea Guzzo", "Intermedio", "Corso in cinque serate del microframework Flask", ) corsoFlask.tags = [t1, t2, t4, t5] # Relazione n:n TAG - CORSO corsoPygame = Corso("Pygame", "Mario Nardi", "Principiante", "Introduzione a Pygame") corsoPygame.tags = [t1, t3, t6] corso_pandas = Corso( "Pandas", "Maria Teresa Panunzio", "Intermedio", "Corso base per manipolare i dataframes", )