Django 1.7 Groups - django

What would be the best method for altering the default group to include a foreign key to a company?
This is using the built - in auth, or would you create a custom auth?
To further update... I would like a many - to - many between groups and company and then a third table for user groups. For example -- company groups (m2m) and user companygroups (m2m)
This will allow organizations to create their own groups if needed...

I have decided to create a m2m between the company table and groups table

Related

Django-ORM: Count number of groups of each user

I know that I can get the number of groups each user has with this query:
User.objects.filter(groups__in=Group.objects.all()).annotate(Count('pk'))
But something is missing:
The users which are in no group at all.
How can I use the django orm to get all users annotated by their group count, incluse users with no group?
You can use Count method with groups attribute:
from django.db.models import Count
User.objects.annotate(group_count=Count('groups'))

How to Create A Dax for RLS?

I have a column named company ID that contains various comapnies with differnet employes in them, I need a DAX Query which will give data A/Q to the Company id, suppose if there are 3 employes inside a company and each of them have a companyid 1 then I they should able to see the reports of each other but they cannot be able to see the reports of comanyid 2 and 3, how this can be achived?
I know I can do this by creating different Roles for each companyid, but how can this be achived if I want this to be built in one particular role???
What you are asking is to implement Dynamic Row Level Security.
Model:
User Table: Table that contains user detail along with the field on which we will apply security(here email field).
Company Table: Table containing company data .
User Company Bridge: Bridge table that contains permission details, for example user x is member of company y and z.
Company Data Table: Measures or transaction information of company that is to be filtered.
Defining RLS(Row Level Security):
In Modelling -> Manage Roles, create a new role on Email of User Table by this DAX query which returns the email id of logged in user.
[Email] = userprincipalname()
Finalizing:
Go to PowerBI Service -> Dataset -> Security and add users to the roles created.
To test the implementation:
Go to Modelling tab of pbix file.
Click on View As Roles.
Check other User checkbox and put an email ID and also check Profile
checkbox.
Now you can see data filtered.
In this manner it becomes easy to maintain roles and security by just modifying the bridge table that stores all permission details.

Removing access of a particular model from a particular user in Django admin, but user is from a group having the access for that model

Suppose user1 is a staff member belonging to a group g1 in django admin. This group g1 is having access to say a model m1. Now I want to remove the access of m1 model from user1 without removing him from the group g1.
How can it be done?
Implementing it in the code seems to be hackish. It is better to create another group g2, add there user1 and delete him/her from g1. This is the only Django way I know.

django models complex query

I have 3 tables say, TextObj, User, SecurityCheck. The third table has a Foreign Key attribute (textobj) referencing TextObj and there is a many-to-many field (sharedWith) from SecurityCheck to User.
class SecurityCheck(models.Model):
textobj=models.ForeignKey(TextObj)
owner=models.CharField(max_length=255)
sharedWith=models.ManyToManyField(User)
def __init__(self,owner,filename,requestingUsername):
self.owner=owner
self.textobj=TextObj.filter(filename=filename)
self.sharedWith.add(User.objects.filter(username=requestingUsername))
I need to do a query which fetches all the instances of Textobj which have a particular user in the sharedWith field and a particular filename(which is an attribute of TextObj)
You can easily do queries that span (reverse) relationship:
TextObj.objects.filter(securitycheck__sharedWith=user, filename="foo")
https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships
Django offers a powerful and intuitive way to “follow” relationships in lookups, taking care of the SQL JOINs for you automatically, behind the scenes. To span a relationship, just use the field name of related fields across models, separated by double underscores, until you get to the field you want.
It works backwards, too. To refer to a “reverse” relationship, just use the lowercase name of the model.

Question about django model API

So, here is what I want to do.
I have a model Staff, that has a foreign key to the User model. I also have a model Match that has a foreign key to the User model.
I want to select how much Matches every Staff has. I don't know how to do that, so far I only got it working for the User model. From Staff, it will not allow to annotate Match.
This is what is working right now
User.objects.annotate(amount=Count("match")).filter(Q(amount__gt=0)).order_by("amount")
And this is what I wanted to do
Staff.objects.annotate(amount=Count("match")).filter(Q(amount__gt=0)).order_by("amount")
And by the way, is there any way to filter the matches? I want to filter the matches by a certain column.
This wont work?
Staff.objects.annotate(ammount=Count("user__match")).filter(Q(ammount__gt=0)).order_by("ammount")
If both Staff and Match have foreign keys to User, but not to each other, there is no such thing as 'how many matches each staff has'. There are several of both Staff and Match for each User, so there's simply no way of knowing which Staff for a user is related to which Match for that same user.
This isn't a limitation of Django - it's a logical limitation, imposed by the way you've structured your relationships.