From 69a88be2474ccbbbaa890612fc222dd0ad319797 Mon Sep 17 00:00:00 2001 From: Kane Kabena Date: Mon, 26 May 2025 13:54:06 +0300 Subject: [PATCH] freebie tracker completed --- lib/__pycache__/models.cpython-312.pyc | Bin 0 -> 5238 bytes lib/alembic/README | 1 + lib/alembic/env.py | 78 ++++++++++++++ lib/alembic/script.py.mako | 24 +++++ lib/debug.py | 5 + lib/freebies.db | Bin 20480 -> 24576 bytes .../__pycache__/env.cpython-312.pyc | Bin 0 -> 2669 bytes ...6253_create_seed_py_for_test_parameters.py | 28 +++++ .../versions/16d3bd88588f_create_tables.py | 28 +++++ .../versions/349255ddbe32_initial_commit.py | 24 +++++ ...revised_seed_py_for_test_parameters_in_.py | 28 +++++ .../6c61e603a123_added_aggregate_methods.py | 28 +++++ ...0ada_create_seed_py_for_test_parameters.py | 28 +++++ .../9561701f4dee_create_freebies_table.py | 37 +++++++ ...eed_py_for_test_parameters.cpython-312.pyc | Bin 0 -> 797 bytes ...16d3bd88588f_create_tables.cpython-312.pyc | Bin 0 -> 755 bytes ...49255ddbe32_initial_commit.cpython-312.pyc | Bin 0 -> 757 bytes ...py_for_test_parameters_in_.cpython-312.pyc | Bin 0 -> 814 bytes ...f48c_create_companies_devs.cpython-312.pyc | Bin 0 -> 1767 bytes ...23_added_aggregate_methods.cpython-312.pyc | Bin 0 -> 775 bytes .../7a71dbf71c64_create_db.cpython-312.pyc | Bin 0 -> 721 bytes ...eed_py_for_test_parameters.cpython-312.pyc | Bin 0 -> 797 bytes ...4dee_create_freebies_table.cpython-312.pyc | Bin 0 -> 1961 bytes ...lists_to_avoid_duplicates_.cpython-312.pyc | Bin 0 -> 812 bytes ..._lists_to_avoid_duplicates.cpython-312.pyc | Bin 0 -> 803 bytes ..._used_unique_lists_to_avoid_duplicates_.py | 28 +++++ ...2_used_unique_lists_to_avoid_duplicates.py | 28 +++++ lib/models.py | 62 ++++++++++- lib/seed.py | 96 ++++++++++++++++++ 29 files changed, 521 insertions(+), 2 deletions(-) create mode 100644 lib/__pycache__/models.cpython-312.pyc create mode 100644 lib/alembic/README create mode 100644 lib/alembic/env.py create mode 100644 lib/alembic/script.py.mako create mode 100644 lib/migrations/__pycache__/env.cpython-312.pyc create mode 100644 lib/migrations/versions/0da7779a6253_create_seed_py_for_test_parameters.py create mode 100644 lib/migrations/versions/16d3bd88588f_create_tables.py create mode 100644 lib/migrations/versions/349255ddbe32_initial_commit.py create mode 100644 lib/migrations/versions/4d687419ba60_revised_seed_py_for_test_parameters_in_.py create mode 100644 lib/migrations/versions/6c61e603a123_added_aggregate_methods.py create mode 100644 lib/migrations/versions/8e26b5ee0ada_create_seed_py_for_test_parameters.py create mode 100644 lib/migrations/versions/9561701f4dee_create_freebies_table.py create mode 100644 lib/migrations/versions/__pycache__/0da7779a6253_create_seed_py_for_test_parameters.cpython-312.pyc create mode 100644 lib/migrations/versions/__pycache__/16d3bd88588f_create_tables.cpython-312.pyc create mode 100644 lib/migrations/versions/__pycache__/349255ddbe32_initial_commit.cpython-312.pyc create mode 100644 lib/migrations/versions/__pycache__/4d687419ba60_revised_seed_py_for_test_parameters_in_.cpython-312.pyc create mode 100644 lib/migrations/versions/__pycache__/5f72c58bf48c_create_companies_devs.cpython-312.pyc create mode 100644 lib/migrations/versions/__pycache__/6c61e603a123_added_aggregate_methods.cpython-312.pyc create mode 100644 lib/migrations/versions/__pycache__/7a71dbf71c64_create_db.cpython-312.pyc create mode 100644 lib/migrations/versions/__pycache__/8e26b5ee0ada_create_seed_py_for_test_parameters.cpython-312.pyc create mode 100644 lib/migrations/versions/__pycache__/9561701f4dee_create_freebies_table.cpython-312.pyc create mode 100644 lib/migrations/versions/__pycache__/c28595cd3935_used_unique_lists_to_avoid_duplicates_.cpython-312.pyc create mode 100644 lib/migrations/versions/__pycache__/f85d30582ce2_used_unique_lists_to_avoid_duplicates.cpython-312.pyc create mode 100644 lib/migrations/versions/c28595cd3935_used_unique_lists_to_avoid_duplicates_.py create mode 100644 lib/migrations/versions/f85d30582ce2_used_unique_lists_to_avoid_duplicates.py diff --git a/lib/__pycache__/models.cpython-312.pyc b/lib/__pycache__/models.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..58f8b9c201e78bd3bcfe5fb6c61157fb16d88b12 GIT binary patch literal 5238 zcmc&&T}&L;6}~h3H~Y_@A;GadiAl`j@ROLiurYQ_V*`o0Ni-dHF0ima_|5>G z6_&QCr8=tB;JPigqNt=#AiJ&e6!)p=Qy==WWJ$oIN~zU8v~R>qrNmD?XMUE2I8jtp z>Xr7)z2~0$d(QdJx%-!JI6$Bz`Z~v7gxcS+;S`TuDg6#A3q&Cbmm+B{&ZYS{pLWGv zX?NV6_QX9L_VFoi+86hwg}7k0UC{Q&{pmnF0ByJ8NpkTZPpF3mZ+UNW*4*(BaQrlM z%U9-vdD2G|;Sy2&G2${M^=C9XMw#Iq)L1ezW(a4gCilvk9OI1;qbXTSW;5z|a?0?H$caftN1>xi zp^22tpl^msBeF^j-yl_0kTm$w$=nO0lhTvTnmm%CQbtbGm_q9=`G*6uyb6w$4>)_`x0?R#Dj|G5{ZXsSn<-xD6jY~xyyY~)LOhs z_s0T;uP2+Hk~6yDPneY?Rfm>2BRIv9X_@KLB-InP4Vb(00!3Yy-U6~fv~u4ZnXvb7 z%GqSXvCRsmvyWc#4wvA^NrHbfLdZpKj+?;Qca)#=SE}3~bCGk-ItVS!}`Zf#k_QqP4f(ABH~&uXP+>={R2O==rp+ck#9P*A`yC z_WH+ly~XH>f^gzXsFl!yepA~eBpg$MHYuy12^!l}z|XnnxH)o*yJ^c`bkDg5NsP}e z2SmH{Fe+0W+kI9Hy7Fe)nag=4#qBfpXMawqyw(=|kS&cp~ zem^@cW++w6A&O#73Zm(x2E!YcLDz9vv(Zxu-(vQ_Or*a_5?}>Gjvw6S69&j&9k#T*cZY> zI-R*XM@N<1M?g-+iSlGKKpOV0*Y8`eX;`nSU9Wp&)9+~rTn=u8NY(Cw&{zT~*qME@ zM5PCy3QSM=8B`VsSa;c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6O?tCFSSP%wnaH##(;IO4d7c6Ob!2+eEVgxJ$)S=DX^+zZ#v_uYsu z`HOp=Ert)}Pu{I=xbAzuacQ9V_zT7AuKekHf%^IB_s5nd?&zye^{)m_=X>r(8m}K& z9=kp9k-pk?ZZ&c~-*?~5d5&=Mmjztof}@_@fQMp8!kS==L4U6NvLjdVm0_oUbyFLSbSvF*OGT{x}V^h iFDsh_KAWewI=F`>O5O-PmH^HxY{{H|tXmxV{ literal 0 HcmV?d00001 diff --git a/lib/alembic/README b/lib/alembic/README new file mode 100644 index 000000000..98e4f9c44 --- /dev/null +++ b/lib/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/lib/alembic/env.py b/lib/alembic/env.py new file mode 100644 index 000000000..3a8b93ced --- /dev/null +++ b/lib/alembic/env.py @@ -0,0 +1,78 @@ +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool +from models import Base +from alembic import context + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, target_metadata=target_metadata + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/lib/alembic/script.py.mako b/lib/alembic/script.py.mako new file mode 100644 index 000000000..55df2863d --- /dev/null +++ b/lib/alembic/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/lib/debug.py b/lib/debug.py index 4f922eb69..fd9742a5e 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -1,9 +1,14 @@ #!/usr/bin/env python3 from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from models import Base, Company, Dev, Freebie from models import Company, Dev if __name__ == '__main__': engine = create_engine('sqlite:///freebies.db') + Session = sessionmaker(bind=engine) + session = Session() + import ipdb; ipdb.set_trace() diff --git a/lib/freebies.db b/lib/freebies.db index 12beb1c963e832db481e7a7493e3029e691ac4dc..3bb33ddc59f01262bd6d0bb8d9489bd41f68c089 100644 GIT binary patch delta 589 zcmZozz}Rqrae}mvb$LYY8!DY*Fuxq7<$DR{d^Y9JJ7Dg?Q@xdyrVIlBfc zK!ibxH8N8)@mK(KoH5WH@jx2YV2Ge70=&TRXX1at!2gN=$!0-;8~hqPOe_qNOz!#l={c$F=Xe-FJpY2!JV#IV`w-Rd z82G>OzuPQm@Q7cDjft5-*eO4WgOQm*+%YFJIhB|H0|N{HY6kw*{P+1U^6vqfI-5U~ zpOKA0nvsz+M7KC2v#3ObNtlI=L6wn{Gq@x(IXksTAs{h1o0W%|jX{`^kWae}lUD+2=q2*UvLL>*&MRtCLzSzi7h3@m(74E((OJNfGQq&5o*EaTlQ r#dk}P$285{DB08^Db2(pd6U8yej#K5UXW1&K-Ks8Cr*%NL*)VhkJ1_o diff --git a/lib/migrations/__pycache__/env.cpython-312.pyc b/lib/migrations/__pycache__/env.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..eb99a8cb126df63f5c9cd3fb2ca1418deac2ba21 GIT binary patch literal 2669 zcmZuz&1)M+6ra_uq}5s<{)pW812cAGJ4$0K&L@=85GQR3EhYpP8ePO{cPy>E+Er&q zwiOa^p@#+vrKcR)L$1X+_8)Mry+qhS&f*+ONuf6vJ2jXd`es+_wOy~!%$xV#yf=Dp ze!taksg#1?`A4{1S(Fj_gD-+d>%&;v78y23l7Zyw?j4E6Vsf=c(#e1MWbFftg@HTkAyW!ywiGHmI>BvYm#BI zL@aC(qQfcf%-*_p7c(1|Al#B_m^?%0m)urO6V7i3$DbxEL~Af|K8L;Zq-HQLTiM_d z9Y($MHrII>9gP}whV2HcH&`d=!?g3YshV1a)SA-`YRHMjWAVLnd+uYgAuBl#gbv^j+@jC)g&4#+I8m4<>GG4ue+P^cBzKNEzm8> zZ=9Pr>%fI*Q_)pAsg`?+b_u!>=ma{}!GPlw|eAL#jtT_gEL)gm`oC4UF% zXxjA}u~=S%{^#oz0L#>se4X-W8Yx0CoqW9s?X;%qSEd#yAti%MK|*bj(7b7s@?ow# zv6iOmO}GE}5sIBVMze4R&IXPK!z$W4i@JK+3T`R*<><_Ya(VUkw$#;@vMnjQmCS6P zo!sgl-0B~D_Swtq#dnElG`5#UN>4j6)=G?RB+mVfgy`tDoN3D=EqP>9#`_{D_PN$G z9XPy3>xt~GNpXELdh1f0=RJH5Y2YE$256webO^Bypb)Aa0{(EBpmsH2GHN&%U60iVkfD)g^j__Il?XDxx&PCidj(4y)TODfYUbGantyia-k-7XGgcn5j#?DY{`G%O>~QGUx@d9Goa z_aKe;_TuOCAWwUE9Db20Z!+zJ0S?yHIRfW8IsiI{Us{YQ@3Wgd2|%xy6oc(Le4U?Q z0Q{v<-}ttiZp(u$dGP1!?V+o!p{pD6wXMwXc5naFsUN5I;!%QvFP}-3m))GRQB+qqEbA7Z>7O zo{7$W80Q_Lgf*x&m+N-Zrd None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/16d3bd88588f_create_tables.py b/lib/migrations/versions/16d3bd88588f_create_tables.py new file mode 100644 index 000000000..eb8723757 --- /dev/null +++ b/lib/migrations/versions/16d3bd88588f_create_tables.py @@ -0,0 +1,28 @@ +"""Create tables + +Revision ID: 16d3bd88588f +Revises: 349255ddbe32 +Create Date: 2025-05-26 13:10:57.060752 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '16d3bd88588f' +down_revision = '349255ddbe32' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/349255ddbe32_initial_commit.py b/lib/migrations/versions/349255ddbe32_initial_commit.py new file mode 100644 index 000000000..d5eb9416a --- /dev/null +++ b/lib/migrations/versions/349255ddbe32_initial_commit.py @@ -0,0 +1,24 @@ +"""initial commit + +Revision ID: 349255ddbe32 +Revises: 5f72c58bf48c +Create Date: 2025-05-24 20:32:36.007700 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '349255ddbe32' +down_revision = '5f72c58bf48c' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + pass + + +def downgrade() -> None: + pass diff --git a/lib/migrations/versions/4d687419ba60_revised_seed_py_for_test_parameters_in_.py b/lib/migrations/versions/4d687419ba60_revised_seed_py_for_test_parameters_in_.py new file mode 100644 index 000000000..2896d7ada --- /dev/null +++ b/lib/migrations/versions/4d687419ba60_revised_seed_py_for_test_parameters_in_.py @@ -0,0 +1,28 @@ +"""revised seed.py for test parameters in reverse + +Revision ID: 4d687419ba60 +Revises: 0da7779a6253 +Create Date: 2025-05-26 13:31:20.909756 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '4d687419ba60' +down_revision = '0da7779a6253' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/6c61e603a123_added_aggregate_methods.py b/lib/migrations/versions/6c61e603a123_added_aggregate_methods.py new file mode 100644 index 000000000..511aa0eb2 --- /dev/null +++ b/lib/migrations/versions/6c61e603a123_added_aggregate_methods.py @@ -0,0 +1,28 @@ +"""added Aggregate Methods + +Revision ID: 6c61e603a123 +Revises: c28595cd3935 +Create Date: 2025-05-26 13:53:09.229066 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '6c61e603a123' +down_revision = 'c28595cd3935' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/8e26b5ee0ada_create_seed_py_for_test_parameters.py b/lib/migrations/versions/8e26b5ee0ada_create_seed_py_for_test_parameters.py new file mode 100644 index 000000000..f1a282d4f --- /dev/null +++ b/lib/migrations/versions/8e26b5ee0ada_create_seed_py_for_test_parameters.py @@ -0,0 +1,28 @@ +"""Create seed.py for test parameters + +Revision ID: 8e26b5ee0ada +Revises: 9561701f4dee +Create Date: 2025-05-26 13:18:43.272155 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '8e26b5ee0ada' +down_revision = '9561701f4dee' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/9561701f4dee_create_freebies_table.py b/lib/migrations/versions/9561701f4dee_create_freebies_table.py new file mode 100644 index 000000000..4e891bb69 --- /dev/null +++ b/lib/migrations/versions/9561701f4dee_create_freebies_table.py @@ -0,0 +1,37 @@ +"""Create freebies table + +Revision ID: 9561701f4dee +Revises: 16d3bd88588f +Create Date: 2025-05-26 13:13:58.993750 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '9561701f4dee' +down_revision = '16d3bd88588f' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('freebies', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('item_name', sa.String(), nullable=True), + sa.Column('value', sa.Integer(), nullable=True), + sa.Column('dev_id', sa.Integer(), nullable=True), + sa.Column('company_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['company_id'], ['companies.id'], name=op.f('fk_freebies_company_id_companies')), + sa.ForeignKeyConstraint(['dev_id'], ['devs.id'], name=op.f('fk_freebies_dev_id_devs')), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('freebies') + # ### end Alembic commands ### diff --git a/lib/migrations/versions/__pycache__/0da7779a6253_create_seed_py_for_test_parameters.cpython-312.pyc b/lib/migrations/versions/__pycache__/0da7779a6253_create_seed_py_for_test_parameters.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dd6c6116ddffde65fa799ed36f59c4a60fd82ba8 GIT binary patch literal 797 zcmaJ<&2G~`5MDcfj+<0PAQdl8KOz)1VejA#6L=39U5c5Q-o^ZG?JGYR>jlfQ;}Wt$Up z7lf05aD$tPk(z;N=(v{Df;z7U4c-WvycV?ZSlr^x&-I{H56kxOHCNra^z>6Y6sZj`6sp0NzZW#mp_2}xe0kd7W5lJ|0k-=^AFjUc^~s9y3o;} zTY9Ey3MdyMoAvfoi&CD4l&Tg>Af3md&|)fEo+m5`BS=@uluTLT?$T9PbJP2DE?5>u zS~N(cvN;rxaY^&6T1x2e|1;jLb&}?Mk-#hQ7^?;z=_6d%48!8nu5GEzrk`+5h(E@2Z6k0tvvZcs&j37(Bbn(zpiy(=YN{IeK$}Zf=L*60J zkZ0)&1ng3vQ@0dumrNz)6g4{Z0DgDJ`{CWk&%K_Bgp4julQRXOZ_@cA!f#}IF4Qd| z$VUVdCBwPmD_GiUrurJu{1$2XZKC?RI0i9D`$qFSLhC}CYqj6`l)R-7)?n3PlmRo% z_pnM?S{By3`OF%Pi5rpW)SFH(cM)JS%RN1FJdcnFT*rJWpNk$d%NaV}VCW5;u{CmM zqv6b(oD9dqiRYN+cR6w&bET{nyu06-clSHS&1!p~3VW*wxJ7GphZlE<8uCh}Jl@zz znS*^57BHz3dk!ne$|{GVw&Rk3T_qudfooSZjO#RpAPXaASL-?{i$UcME-65i!k`xM zE1-6kMs}WlpkXaIVD<{AB!k)e^9FH4S+Jw*q*`-*S&0$?^gzt?Tn^U~ut8rngLZ`V z|DC8VQktE1AS9X}YT`+8&CAHCW1?vGvw=snw*+qQx`Kf3~& GeSQNov%o$8 literal 0 HcmV?d00001 diff --git a/lib/migrations/versions/__pycache__/349255ddbe32_initial_commit.cpython-312.pyc b/lib/migrations/versions/__pycache__/349255ddbe32_initial_commit.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..fda739a346768f0ec15b459f5e01b8b5a7baa2f0 GIT binary patch literal 757 zcmaJIVKyFj`Y}qk6q7w~iXi`wRv25=Iug-t0ouE*qfM>X8 zxSqG*1)`;-qB|65u3|R{5K*v_zxifoc6R1lfA059B-SUz%W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)BG-%?_;hM<&1atC-d(9#Hb;;2deP5nt%(mMwfVY ziI^cD$&#B-c3fm&pOYM(*0DW>6{JO(L0;QYLBTF#Qo+ErOGctPi6BhLyt2!+V4Dw0 zckq@0%o7;YB7Y9dPLsKvB@0Gsp+RM@fJrebdw<`s5icaS;)U5xypwXx^<^oVDX;^v z-Cen6OTY$w)eP1V-v8M#U6eHJb|5>L{3CdW7Vq=jkcS<+W{n?b!~`K*7y z^)RGG6oy~KezI)~iS-jt)7KcGFEaSOl%umh7tR%; zC`1%fCC7ylDp>kzu7(=b!U0u7U2H>alNxmJQ45E{>cU!`IyOJNNf?k892#5JE7)dD zR+Y}n)zVpzg-_#)g`doy^%-cEj`gB1Ux_nIXF8eA+{w)K1IG`RlVBMHXMsPT&wR`J zDoKtpSE_o=Pw#h2j`aT+Rd{<%z!mDyHC|sMX2?&n;BM!oRRP{5DPhs3-U_ymS9Jkp z>m?NhuTDt=?#!zhN!l!dC?|2_)m@ubrCZP33kDEp;I<-v3Czp0*ekNPjI=^&<86US z$&GiPVbl*LqVZYXabr`9atch|$YV)tAYg}XG?SUa`@bHhi;{z$9^?r&6u^hHdSCXP zZ4r-T;zWWh87o|8&~8|{9-nX{ifENY5jRKlf!c+nTa=unyG+W6=VEO(Xsa9 j0m7rtXmo3$)2BQ0=HZi_-GlvTXYF;2hu=*B2R*+4mA1BS literal 0 HcmV?d00001 diff --git a/lib/migrations/versions/__pycache__/8e26b5ee0ada_create_seed_py_for_test_parameters.cpython-312.pyc b/lib/migrations/versions/__pycache__/8e26b5ee0ada_create_seed_py_for_test_parameters.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..620c04acbbbc9ee6ad21542ffac480fc4a4dd2a7 GIT binary patch literal 797 zcmaJ<&2G~`5MJA9;Ili4!*^st^*raLIwAFRP7rimm#OU8g8WNICEh z6_3zo;RUMYg2ahiQQ*=OvrbVX4ve(l%y`D1XTJ59Zr34b*2{4`GYR>wlfQ;}ZR-3DSLPyAsJIL;5fN6%1ZQO;Cnn^|?X8KZjNEwC_N zHxMq#l3W?HD){$x*X}P4RBh@<6 zdmT|;)moIu!$Iugpbz!LI|wWCtznBcM*m+_w6LXKbOZVgq5l)r=EeJb!@P(26J6+N z&@DYvH3d`)kq2A{YeA3c& literal 0 HcmV?d00001 diff --git a/lib/migrations/versions/__pycache__/9561701f4dee_create_freebies_table.cpython-312.pyc b/lib/migrations/versions/__pycache__/9561701f4dee_create_freebies_table.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..469f54a42a9ffe8f211080e790bfff219f8c55c3 GIT binary patch literal 1961 zcmb_dO>7%Q6y9C0*XxZPQiv3_fncg0Fv8iyKZ$LkA~jJ(OzR@xP%Ol1y)(8q-als7 zMz%O45)vFZv}&a!N~CZ!N)NpvAt59r4hShIMpOegkPx>b(@Rgxu6Jv4QOogFcIi!UuEMO~P z5Uy|_REdzu10Hanu$3s`5yFK|C>iZqs|d;tOwkuRQx18HbrQN%)u8V_if6d1LH%7T4{;M+RU5kHhHDL{l9q z`Aiw}!F|Gv3NDg%=Z%)8xmOEERR`*lA?xs69B#{63t|qywxj|qD5h@7Mu*7V*ia@{ za-LWcwWbt6N~5oQk$gl#ur6jdLa;oSnQ@n40Lir_qsVP)989g5DtLF=}OW{=+3I9_e`S#9*e)tyrBbZ_G8v)`Qi>fF)mSC5PD_1VhEzIM#MbnjF&%%L+R zqley2_n_T5m?z}P?`F`=UKcZE6d_(oX4ozQK&CZp7Id2EyjvPF|8IojZYXKAtRmS^8gA!6&Bg+N7Bqk@nTB`F$lcB(yS>k^=q6}s@H~2%SlySz zP7`uBMAP)&3`eu~CMkO6S8C=iPsPq`@^_}@Hsg1urU{9k+7t%d1kL?3$Ix?LLjMB# CldhHk literal 0 HcmV?d00001 diff --git a/lib/migrations/versions/__pycache__/c28595cd3935_used_unique_lists_to_avoid_duplicates_.cpython-312.pyc b/lib/migrations/versions/__pycache__/c28595cd3935_used_unique_lists_to_avoid_duplicates_.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9a445b869d40ac7cb3867a8921dd1aff02d92ca2 GIT binary patch literal 812 zcmaJdHYu~pmW-q}H6%D})k z=)?!;XWR7r$Tr~C^-^%)dUE>e#iVDNFJT_@m}b`LWNd}b&>MLnaYwGV zt^glfXG4#;``*wA!7-mRkObC5!q{^59k0Ld^__ur;EoT6W9MjZ;2jJ`o@stlEA=#? z(|jsAo6MrK$rx6vZ$M4PtGa|sv_Mzd^a?RUJyI>VAMJ>yV4nvW92Jp0fjJ~JPa!Mp zkP@)-DB#d{?VJT+5r=@2V8-oyQA9NB=WhRu0nB3P7qWg1%ueE&oyKoiP)H59JqM=5 z;P&RcSV@Z4lHy8ITu@v|iti-Fd-+1N%3N|0U|Vw7`)cZjgeCe?H&|On|6i81WJ|T| z8q^z1{WhXa=-X_~ye;!bs?brOT6(%@GAK%xO?z9Sg)yNa#-bG@kj~|XCYkkGxiG*?YsYyap-sP_2{De=~u literal 0 HcmV?d00001 diff --git a/lib/migrations/versions/__pycache__/f85d30582ce2_used_unique_lists_to_avoid_duplicates.cpython-312.pyc b/lib/migrations/versions/__pycache__/f85d30582ce2_used_unique_lists_to_avoid_duplicates.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dc347774ebac4cfdaef8000f1d4994228f45e3b5 GIT binary patch literal 803 zcmaJ zvI09zc**UO+QE`0#3ogdCQ(oVx2!j?On5?b`_hdb2)u0u_}m_m$!t10 zJ_&<~YrS9~587uE=CdK?}|!a6FMyxqPyE!bay*O zHRT;>NPlBUxI!y*tu3w*Gu10qa`(}RX$Hk}37sr?%YssBz?~&9B?fnP=fz4&yp0T1tE`q9kkQ^Fd3F-+Ex-v7C>wk)Z+Zb6-3>TeMpLf_@v z&igWcq7q#Ns-&-r7K5^4`J%rk+87fWVJzA~3R##$Oz9~sewhYo6hpQWIuC@&{@lCh zsowZL3|WvzvC<7vE-V5CqXLNXLqVChRd2{e=-Mcw>B%$|UZEae**7@0$Q1kf>!9vtK literal 0 HcmV?d00001 diff --git a/lib/migrations/versions/c28595cd3935_used_unique_lists_to_avoid_duplicates_.py b/lib/migrations/versions/c28595cd3935_used_unique_lists_to_avoid_duplicates_.py new file mode 100644 index 000000000..ba23e436e --- /dev/null +++ b/lib/migrations/versions/c28595cd3935_used_unique_lists_to_avoid_duplicates_.py @@ -0,0 +1,28 @@ +"""used unique lists to avoid duplicates (FIXED) + +Revision ID: c28595cd3935 +Revises: f85d30582ce2 +Create Date: 2025-05-26 13:48:27.651695 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'c28595cd3935' +down_revision = 'f85d30582ce2' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/f85d30582ce2_used_unique_lists_to_avoid_duplicates.py b/lib/migrations/versions/f85d30582ce2_used_unique_lists_to_avoid_duplicates.py new file mode 100644 index 000000000..525ceead4 --- /dev/null +++ b/lib/migrations/versions/f85d30582ce2_used_unique_lists_to_avoid_duplicates.py @@ -0,0 +1,28 @@ +"""used unique lists to avoid duplicates + +Revision ID: f85d30582ce2 +Revises: 4d687419ba60 +Create Date: 2025-05-26 13:43:49.700062 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f85d30582ce2' +down_revision = '4d687419ba60' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/models.py b/lib/models.py index 2681bee5a..b98a641dc 100644 --- a/lib/models.py +++ b/lib/models.py @@ -1,6 +1,6 @@ from sqlalchemy import ForeignKey, Column, Integer, String, MetaData from sqlalchemy.orm import relationship, backref -from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import declarative_base, Session convention = { "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", @@ -16,6 +16,32 @@ class Company(Base): name = Column(String()) founding_year = Column(Integer()) + @property + def devs(self): + return list({freebie.dev for freebie in self.freebies.all()}) + + + def give_freebie(self, dev, item_name, value): + """ + Create and return a Freebie given to a dev from this company. + Note: You need to add and commit the freebie in your session outside this method. + """ + new_freebie = Freebie( + item_name=item_name, + value=value, + dev=dev, + company=self + ) + return new_freebie + + @classmethod + def oldest_company(cls, session: Session): + """ + Return the company instance with the earliest founding year. + Requires a SQLAlchemy session as argument. + """ + return session.query(cls).order_by(cls.founding_year.asc()).first() + def __repr__(self): return f'' @@ -23,7 +49,39 @@ class Dev(Base): __tablename__ = 'devs' id = Column(Integer(), primary_key=True) - name= Column(String()) + name = Column(String()) + + @property + def companies(self): + return list({freebie.company for freebie in self.freebies.all()}) + + def received_one(self, item_name: str) -> bool: + return any(freebie.item_name == item_name for freebie in self.freebies.all()) + + def give_away(self, dev, freebie): + if freebie in self.freebies.all(): + freebie.dev = dev + return True + return False def __repr__(self): return f'' + +class Freebie(Base): + __tablename__ = 'freebies' + + id = Column(Integer, primary_key=True) + item_name = Column(String) + value = Column(Integer) + + dev_id = Column(Integer, ForeignKey('devs.id')) + company_id = Column(Integer, ForeignKey('companies.id')) + + dev = relationship("Dev", backref=backref("freebies", lazy="dynamic")) + company = relationship("Company", backref=backref("freebies", lazy="dynamic")) + + def __repr__(self): + return f'' + + def print_details(self): + return f"{self.dev.name} owns a {self.item_name} from {self.company.name}." diff --git a/lib/seed.py b/lib/seed.py index b16becbbb..c7276282a 100644 --- a/lib/seed.py +++ b/lib/seed.py @@ -1,3 +1,99 @@ #!/usr/bin/env python3 # Script goes here! +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from models import Base, Company, Dev, Freebie # All models from models.py + +engine = create_engine('sqlite:///freebies.db') +Session = sessionmaker(bind=engine) +session = Session() + +# clear old data for testing +session.query(Freebie).delete() +session.query(Dev).delete() +session.query(Company).delete() + +# Create sample companies +company1 = Company(name="OpenAI", founding_year=2015) +company2 = Company(name="Google", founding_year=1998) + +# Create sample devs +dev1 = Dev(name="Alice") +dev2 = Dev(name="Bob") + +# Create sample freebies +freebie1 = Freebie(item_name="T-shirt", value=20, dev=dev1, company=company1) +freebie2 = Freebie(item_name="Mug", value=10, dev=dev2, company=company2) +freebie3 = Freebie(item_name="Sticker", value=5, dev=dev1, company=company2) + +# Add and commit +session.add_all([company1, company2, dev1, dev2, freebie1, freebie2, freebie3]) +session.commit() + +# Testing relationships +freebie = session.query(Freebie).first() +print(f"{freebie.item_name} was given by {freebie.company.name} to {freebie.dev.name}") + +dev = session.query(Dev).first() +print(f"{dev.name} has received the following freebies:") +for f in dev.freebies: + print("-", f.item_name) + +company = session.query(Company).first() +print(f"{company.name} has given out the following freebies:") +for f in company.freebies: + print("-", f.item_name) + + +# Testing relationships in reverse +company = session.query(Company).first() +print(f"{company.name} has given freebies to:") +for dev in company.devs: + print("-", dev.name) + +dev = session.query(Dev).first() +print(f"{dev.name} has received freebies from:") +for company in dev.companies: + print("-", company.name) + +#testing aggregate functions +freebie = session.query(Freebie).first() +print(freebie.print_details()) + +# Find a dev and a company +dev = session.query(Dev).filter_by(name="Alice").one() +company = session.query(Company).filter_by(name="OpenAI").one() + +# Give a freebie +freebie = company.give_freebie(dev, "Sticker Pack", 5) +session.add(freebie) +session.commit() + +# Get the oldest company +oldest = Company.oldest_company(session) +print(f"Oldest company is {oldest.name}, founded in {oldest.founding_year}") + +# Fetch two devs and one freebie owned by the first dev +dev1 = session.query(Dev).first() +dev2 = session.query(Dev).filter(Dev.id != dev1.id).first() +freebie_to_give = dev1.freebies.first() # Using .first() from lazy='dynamic' + +print(f"Before give_away:") +print(f"{dev1.name} has freebie: {freebie_to_give}") +print(f"Does {dev1.name} have 'Sticker'? {dev1.received_one('Sticker')}") + +# Dev1 gives away the freebie to dev2 +if freebie_to_give: + success = dev1.give_away(dev2, freebie_to_give) + if success: + session.commit() + print(f"\nAfter give_away:") + print(f"Freebie {freebie_to_give.item_name} now belongs to {freebie_to_give.dev.name}") + else: + print("Failed to give away freebie (ownership mismatch)") +else: + print("No freebies found for dev1") + +# Close the session +print("✅ Database seeded successfully!") \ No newline at end of file