
    f                     d    d dl Z d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ  G d d      Zy)	    N)Path)apps)settings)Seeder)AppliedSeederc                   h    e Zd ZdZg Zed        Zed        Zedd       Zedd       Z	ed        Z
y)	SeederRegistryz
    The `SeederRegistry` class apply registered seeders when the server is run.

    seeder registering is doing by:
        @SeederRegistry.register as <decorator>
        or
        SeederRegistry.register(<seeder-class>) as <method>
    c                     t        |t              st        d      | j                  D ]*  }|j	                          |       j	                         k(  s* y | j                  j                   |              y)zk Method and decorator to register the seeder-class in the seeders list to be seeded when the server is run zNOnly subclasses of Seeder class can be registered with SeederRegistry.registerN)
issubclassr   	TypeErrorseeders_get_idappend)clsseeder
cur_seeders      r/var/www/teaching_timetable/msuas_timetable/my_venv/lib/python3.12/site-packages/django_seeding/seeder_registry.pyregisterzSeederRegistry.register   sb     &&)lmm++ 	J!!#vx'7'7'99	 	68$    c                 ~   t        j                         D ]  }|j                  }d}|dz   }t        |j                        |z  }|j                         s?t        j                  j                  | d| t        |            }t        j                  j                  |      }|j                  j                  |        y)zp Method that import all `seeders.py` files in the installed apps to register them in the `SeederRegistry` class r   z.py.N)r   get_app_configsnamer   pathexists	importlibutilspec_from_file_locationstrmodule_from_specloaderexec_module)r   
app_configapp_namemodule_name	file_name	file_pathspecmodules           r   
import_allzSeederRegistry.import_all!   s     ..0 	0J!H#K#e+IZ__-	9I! ~~==
!K=>Y[^_h[ij"88>''/	0r   Nc                    | j                   }t        t        fd|            }t        j                  j                  |D cg c]  }|j                          c}      j                         t        |      k7  rd}d}t        |dz   |z          |j                  d        |D ]  }|j                  |	        yc c}w )
z 
        Method that call seed methods for all registered seeders
        
        sort the seeders depending on the `priority` (less is applied earlier)
        Nc                 (    | j                         v S N)r   )r   idss    r   <lambda>z)SeederRegistry.seed_all.<locals>.<lambda>7   s    1AS1H r   )id__inz[94mz[0mzRunning Seeders: c                 "    | j                         S r-   )_get_priority)r   s    r   r/   z)SeederRegistry.seed_all.<locals>.<lambda>>   s    (<(<(> r   )key)debug)r   listfilterr   objectsr   countlenprintsort_seed)r   r4   r.   r   r   
BLUE_COLORWHITE_COLORs     `    r   seed_allzSeederRegistry.seed_all.   s     ++?6"H'RSG  ''w/WV0@/W'X^^`dghodpp#J#K*22[@A>? 	&FLLuL%	& 0Xs   C c                 J    | j                          | j                  ||       y)a  
        Note: the decorator `@SeederRegistry.register` will be applied when the file is imported

        so, if the seeder class is written in another file (not in `seeders.py`)
        then it will not be imported
        then it will not be applied when the server is run

        so, to solve this problem you can import any file contains seeder inside the `AppConfig` class of your app
        )r4   r.   N)r*   r?   )r   r4   r.   s      r   import_all_then_seed_allz'SeederRegistry.import_all_then_seed_allB   s      	 	5c*r   c                     dt         j                  vry t        t        t        dd            }dt         j                  v rd}dt         j                  v rd}|r| j                          y y )N	runserverSEEDING_ON_RUNSERVERFz--seedTz--dont-seed)sysargvboolgetattrr   rA   )r   seeds     r   on_runzSeederRegistry.on_runT   s[    chh&GH&<eDEsxxDCHH$D((* r   )NN)__name__
__module____qualname____doc__r   classmethodr   r*   r?   rA   rJ    r   r   r	   r	   
   sp     G	% 	% 
0 
0 & && + +" + +r   r	   )rE   importlib.utilr   pathlibr   django.appsr   django.confr   django_seeding.seedersr   django_seeding.modelsr   r	   rP   r   r   <module>rW      s&    
      ) /X+ X+r   