Package: edc-sites
edc-sitesSite definitions to work with Django’sSites Frameworkanddjango_multisite.Define asites.py. This is usually in a separate project module. For example, for projectmetathere is a modulemeta_sitesthat contains asites.py.Register your sites insites.py.# sites.pyfromedc_sites.siteimportsitesfromedc_sites.single_siteimportSingleSitesuffix="example.clinicedc.org"sites.register(SingleSite(10,"hindu_mandal",title="Hindu Mandal Hospital",country="tanzania",country_code="tz",domain=f"hindu_mandal.tz.{suffix}",),SingleSite(20,"amana",title="Amana Hospital",country="tanzania",country_code="tz",domain=f"amana.tz.{suffix}",),)Apost_migratesignal is registered inapps.pyto update the django modelSiteand the
EDC modelSiteProfileon the next migration:# apps.pyfromedc_sites.add_or_update_django_sitesimportadd_or_update_django_sitesdefpost_migrate_update_sites(sender=None,**kwargs):sys.stdout.write(style.MIGRATE_HEADING("Updating sites:\n"))add_or_update_django_sites(verbose=True)sys.stdout.write("Done.\n")sys.stdout.flush()Now in your code you can use thesitesglobal to inspect the trial sites:fromedc_sites.siteimportsitesIn[1]:sites.all()Out[1]:{10:SingleSite(site_id=10,name='hindu_mandal',domain='hindu_mandal.tz.example.clinicedc.org',country='tanzania',description='Hindu Mandal Hospital'),20:SingleSite(site_id=20,name='amana',domain='amana.tz.example.clinicedc.org',country='tanzania',description='Amana Hospital')}In[2]:sites.get(10)Out[2]:SingleSite(site_id=10,name='hindu_mandal',domain='hindu_mandal.tz.example.clinicedc.org',country='tanzania',description='Hindu Mandal Hospital')In[3]:sites.get_by_attr("name",'hindu_mandal')Out[3]:SingleSite(site_id=10,name='hindu_mandal',domain='hindu_mandal.tz.example.clinicedc.org',country='tanzania',description='Hindu Mandal Hospital')In[4]:sites.get(10).languagesOut[4]:{'sw':'Swahili','en-gb':'British English','en':'English','mas':'Maasai','ry':'Runyakitara','lg':'Ganda','rny':'Runyankore'}Take a look at theSitesclass in edc_sites.site for more available methods.For another deployment, we have alot of sites spread out over a few countries.For example:fromedc_sites.siteimportsitesfromedc_sites.single_siteimportSingleSitesuffix="inte.clinicedc.org"sites.register(SingleSite(101,"hindu_mandal",title="Hindu Mandal Hospital",country="tanzania",country_code="tz",domain=f"hindu_mandal.tz.{suffix}",),SingleSite(102,"amana",title="Amana Hospital",country="tanzania",country_code="tz",domain=f"amana.tz.{suffix}",),SingleSite(201,"kojja",country="uganda",country_code="ug",domain=f"kojja.ug.{suffix}",),SingleSite(202,"mbarara",country="uganda",country_code="ug",domain=f"mbarara.ug.{suffix}",),)You can use thesitesglobal to get the trial sites for a country:fromedc_sites.siteimportsitesIn[1]:sites.get_by_country("uganda")Out[1]:{201:SingleSite(site_id=201,name='kojja',domain='kojja.ug.inte.clinicedc.org',country='uganda',description='Kojja'),202:SingleSite(site_id=202,name='mbarara',domain='mbarara.ug.inte.clinicedc.org',country='uganda',description='Mbarara')}In a multisite, multi-country deployment, managing the SITE_ID is complicated. We usedjango_multisitewhich nicely reads
the SITE_ID from the url. django_multisite will extractkojjafromhttps://kojja.ug.example.clinicedc.orgto do a model lookup
to get the SITE_ID.Viewing data from multiple sites usingview_auditallsitesThe mixins provided by``edc_sites`` limit the EDC to only present data linked to the current site.
To expand access beyond the current site,edc_sitesprovides a special permission codename;view_auditallsites. If a user has this permission, they will be shown data from the current
site plus any additional sites granted in their user profile.The permission codenameview_auditallsitescannot be allocated to a user with add/edit/delete
permissions to ANY model in the system. That is, the permission codenameview_auditallsitesis reserved for VIEW ONLY access, e.g the AUDITOR_ROLE. The one exception is foredc_authand``auth`` models accessible to users granted ACCOUNT_MANAGER_ROLE permissions.In your code, you can check if a user has access to more than just the current site using functionmay_view_other_sites:ifmay_view_other_sites(request):queryset=self.appointment_model_cls.objects.all()else:queryset=self.appointment_model_cls.on_siteTo get a list of sites that the user has access to in the current request, use functionget_view_only_site_ids_for_user.fromedc_model_admin.utilsimportadd_to_messages_oncesite_ids=get_view_only_site_ids_for_user(request.user,request.site,request=request)Default Site and testsEdc sites may be configured to register a default site. This may be useful for testing where
you are not registering any sites manually or throughautodiscover.Insettings:EDC_SITES_REGISTER_DEFAULT=TrueThe default site id is 1.If your tests depend on a test app that has asites.py, you might need to set the SITE_ID in your tests.Use theoverride_settingsdecorator on the test class or on a specific test.For example:@override_settings(SITE_ID=20)classTestLpFormValidator(TestCase):defsetUp(self):...@override_settings(SITE_ID=40)deftest_lp_not_done(self):...