On 23/04/2019 9:22 pm, Balaji Shetty wrote:
Hi
Did you get any particular solution. I am also facing the same 
problem. My project also have same requirement.
I think this problem must have been faced by every dveloper.

Can you please share the solution or any useful internet resource .
class UserProfile(models.Model):

    user = models.OneToOneField(User, related_name='userprofile',

        on_delete=models.CASCADE)

    company = models.ForeignKey('Company', null=True, blank=True,

        on_delete=models.CASCADE,

        help_text='Each user may only be associated with '

        '<strong>one</strong> company.')

*...*

    def __str__(self):

        grps = u''

        superuser = u''

        if self.user.is_staff:

            superuser = u' [Staff]'

        if self.user.is_superuser:

            superuser = u' [Superuser]'

        if not self.user.is_active:

            superuser += u' [Inactive]'

        groups = Group.objects.filter(user=self.user).order_by('name')

        if groups:

            grps = u', '.join(x.name for x in groups)

        if grps:

            grps = u' (%s)' % grps

        grps += superuser

        return u'%s%s' % (self.user, grps)

And in company admin.py

class CompanyAdmin(admin.ModelAdmin):

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    #
    def get_queryset(self, request):
        qs = super(CompanyAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        coy = get_user_company(request)
        if coy:
            return qs.filter(
                Q(id=coy.id)
            ).distinct()
    #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

And imported from common utils ...

def get_user_company(request): """Return the company of which the user is a member. Company is kept in the UserProfile fk company. Typically used in get_queryset.filter() calls made in various places to restrict a user's view to things owned by that user's company. """ profile = get_userprofile(request) if profile: try: return profile.company except Exception: # happens on deleting the company pass  def get_userprofile(request): """Return the userprofile from a user or request""" from company.models import UserProfile # avoid circular imports if isinstance(request, User): usr = request else: usr = request.user if usr: try: return UserProfile.objects.get(user=usr) except Exception: # happens on deleting the user pass
And determining which fields are readonly depending on group membership 
is a matter of writing a callable which replaces 
ModelAdmin.get_readonly_fields and for which you need ...
def is_member(user, name): return user.groups.filter(name=name).exists() 
# or user.is_superuser
hth



On Sun, Apr 21, 2019 at 4:37 PM Joel Mathew <j...@eyrie.in 
<mailto:j...@eyrie.in>> wrote:
    Thanks a lot, guys.

    On Sun, 21 Apr, 2019, 3:58 PM Jani Tiainen, <rede...@gmail.com
    <mailto:rede...@gmail.com>> wrote:

        Hi,

        We do in our applications pretty much same approach as Mike
        proposed in his post. It's relatively efficient and works for
        both, UI and application side pretty well and doesn't require
        building anything complex and pretty much everything you need
        is already built-in in Django.



        On Sun, Apr 21, 2019 at 5:12 AM Joel Mathew <j...@joel.su
        <mailto:j...@joel.su>> wrote:

            I have an application for a hospital. There, I should be
            able to assign different roles like Doctor, Nurse,
            Attender, Auxillary Nurse, Pharmacist, Store Manager etc,
            each of would be having specific access to seperate areas
            (views), with some having restricted access. In addition,
            there are other hospitals who would be having no access to
            another hospital's records. All of these permissions
            should be customisable, and I should be able to create
            additional roles and permission groups for specific areas
            later (from within the application itself, in production).
            What would be the best solution to use? Is there a
            middleware which works well. I don't want to roll my own
            if I will be reinventing the wheel.

            Currently my application has no use permissions. But
            access to specific hospitals is being restricted by a
            model whoch stores hospital name, and user name.
            Sincerely yours,

             Joel G Mathew

-- You received this message because you are subscribed to
            the Google Groups "Django users" group.
            To unsubscribe from this group and stop receiving emails
            from it, send an email to
            django-users+unsubscr...@googlegroups.com
            <mailto:django-users+unsubscr...@googlegroups.com>.
            To post to this group, send email to
            django-users@googlegroups.com
            <mailto:django-users@googlegroups.com>.
            Visit this group at
            https://groups.google.com/group/django-users.
            To view this discussion on the web visit
            
https://groups.google.com/d/msgid/django-users/CAA%3Diw_9BrOEh4Ss3X0g_EnaFyp1XTXij5DoZvva_XxwKVzXsGQ%40mail.gmail.com
            
<https://groups.google.com/d/msgid/django-users/CAA%3Diw_9BrOEh4Ss3X0g_EnaFyp1XTXij5DoZvva_XxwKVzXsGQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
            For more options, visit https://groups.google.com/d/optout.



-- Jani Tiainen
        Software wizard

        https://blog.jani.tiainen.cc/

        Always open for short term jobs or contracts to work with Django.
-- You received this message because you are subscribed to the
        Google Groups "Django users" group.
        To unsubscribe from this group and stop receiving emails from
        it, send an email to django-users+unsubscr...@googlegroups.com
        <mailto:django-users+unsubscr...@googlegroups.com>.
        To post to this group, send email to
        django-users@googlegroups.com
        <mailto:django-users@googlegroups.com>.
        Visit this group at https://groups.google.com/group/django-users.
        To view this discussion on the web visit
        
https://groups.google.com/d/msgid/django-users/CAHn91od2vbV7F0WwAVp6Eppqmmd5-NgZciO_04Tm%3D9x9FSJyvQ%40mail.gmail.com
        
<https://groups.google.com/d/msgid/django-users/CAHn91od2vbV7F0WwAVp6Eppqmmd5-NgZciO_04Tm%3D9x9FSJyvQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
        For more options, visit https://groups.google.com/d/optout.

-- You received this message because you are subscribed to the Google
    Groups "Django users" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to django-users+unsubscr...@googlegroups.com
    <mailto:django-users+unsubscr...@googlegroups.com>.
    To post to this group, send email to django-users@googlegroups.com
    <mailto:django-users@googlegroups.com>.
    Visit this group at https://groups.google.com/group/django-users.
    To view this discussion on the web visit
    
https://groups.google.com/d/msgid/django-users/CAA%3Diw_-6cLiAfN1NHovdY4-Y_Wtz1KToMWW6zErNKAdXNOpm1w%40mail.gmail.com
    
<https://groups.google.com/d/msgid/django-users/CAA%3Diw_-6cLiAfN1NHovdY4-Y_Wtz1KToMWW6zErNKAdXNOpm1w%40mail.gmail.com?utm_medium=email&utm_source=footer>.
    For more options, visit https://groups.google.com/d/optout.



--
/Mr. Shetty Balaji S.
Asst. Professor
Department of Information Technology,/
*/SGGS Institute of Engineering & Technology, /Vishnupuri, Nanded.MH.India//*
*Official: bsshe...@sggs.ac.in <mailto:bsshe...@sggs.ac.in> *
*// Mobile: +91-9270696267*

--
You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com <mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@googlegroups.com <mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAECSbOtdC_wg-ceB6-dvWcS6z%2BoWkwm%2BzunBW3GR_Kd0G3GsrA%40mail.gmail.com <https://groups.google.com/d/msgid/django-users/CAECSbOtdC_wg-ceB6-dvWcS6z%2BoWkwm%2BzunBW3GR_Kd0G3GsrA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/717fee4d-45a4-8502-eb82-1e69106e979e%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Reply via email to