0

I'm building an app in which users will be able to upload data for specific "sites", defined by the class below:

class Site(models.Model):
    name = models.CharField(max_length=150)
    code = models.CharField(max_length=10)
    date_added = models.DateTimeField(default=timezone.now)
    latitude = models.FloatField()
    longitude = models.FloatField()

    class Meta:
        permissions = (
            ('upload_data', 'Upload Data'),
            ('download_data', 'Download Data')
        )

Each Site will have "Data" objects associated with it, which users will need permission to create. For instance, 'User A' should only be able to upload data for 'Site A', and 'User B' to 'Site B'. How do I implement this into the Site model and on the User model side?

What I would like is a way from the admin panel to easily assign users to a site (or multiple sites).

ad_intra
  • 93
  • 8
  • 1
    Did you give it a try? It's better to try something first and come up with a specific question about the libraries issues or integration issues. – Toan Quoc Ho May 19 '20 at 05:11
  • I have it "working" on the backend. In the shell: ``` >>>user1 = User.objects.filter(username='Site1User').first() >>>site1 = EonsSite.objects.filter(code='E001').first() >>>user1.has_perm('upload_data', site1) False >>>from guardian.shortcuts import assign_perm >>>assign_perm('upload_data', user1, site1) >>>user1.has_perm('upload_data', site1) True ``` But there is no indication that this does anything on the front end. All users are still able to access the upload view. – ad_intra May 19 '20 at 05:50
  • This library just create the permission mechanism and it has nothing to do with the view permission so I'm afraid that you have to handle it by yourself. For example, you have to create a middleware and implement as you do above. If users have permissions then they can access to the view but if they don't have the permissions then you can throw Exception to stop them access to the view – Toan Quoc Ho May 19 '20 at 09:48

0 Answers0