Re: SOLUTION FounD : How to restore Models original Properties - Problem in Models column Exclude

2019-05-31 Thread Alexis Roda
Hi Balaji,

Sorry but I don't understand what do you mean with "*If we put this code
with necessary care*".

All I can say is that your code is not thread safe and it *may* break. How
likely is this to happen depends on the usage patterns of your app. How
serious it will be depends on the leaked data. Depending on
context/constraints it might be a perfectly *good enough* solution. It's up
to you deciding if it's worth fixing it.

That being said, overriding get_exclude works for me: it hides/shows some
fields in an admin form depending on some condition (and it's thread safe).
It requires django 1.11 or newer and it may be affected by fields and
readonly_fields defined in the model admin and in the model form. Please,
provide the relevant code, errors etc. if you require further assistance
with this.

Another approach would be overriding get_form as shown in the django
docs[1].

The point is that neither of those solutions makes changes to shared state
(the admin).


HTH

[1] https://docs.djangoproject.com/en/1.11/ref/contrib/admin/

Missatge de Balaji Shetty  del dia dv., 31 de maig
2019 a les 16:42:

> Dear Alexis Roda Sir
>
> Thank You very Much.
>
> definitely Race Condition may arise if we do not put code inn critical
> section. But your suggested approach did not work and i found another
> alternative which is working fine. If we put this code with necessary care
> than will it be ok?
>
>
>
> On Tue, May 28, 2019 at 8:33 PM Alexis Roda <
> alexis.roda.villalo...@gmail.com> wrote:
>
>> No, it isn't. It's not thread safe.
>>
>> Changes in shared state in multi-threaded code may lead to race
>> conditions[1].
>>
>> Depending on your usage it may be very unlikely, but under the right
>> circumstances root may end up having some missing fields in the form or dgp
>> may see fields that it is not intended to see.
>>
>> Either protect the critical section[2] with some kind of lock or, as I
>> suggested in my previous answer, create non-shared (request local) state.
>>
>>
>> [1] https://en.wikipedia.org/wiki/Race_condition
>> [2] https://en.wikipedia.org/wiki/Critical_section
>>
>> --
>> 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/CAMDYoXa16C5goSadFbSoXQg1-v2zh9vrq%2BAk%3Dkqbq1rk%3DsgSQw%40mail.gmail.com
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> --
>
>
> *Mr. Shetty Balaji S.Asst. ProfessorDepartment of Information Technology,*
> *SGGS Institute of Engineering & Technology, Vishnupuri, Nanded.MH.India*
> *Official: 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.
> 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/CAECSbOvKUK0H2mrt_S%2BhmbVmmeneQXXc%2BR9a%3DFGHE7XOVmOK-Q%40mail.gmail.com
> 
> .
> 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/CAMDYoXb%3D7D99YNQrAos%2BVWaxqxq%2B4z9m_kw3nLPZ28bfrZ-Ujw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: SOLUTION FounD : How to restore Models original Properties - Problem in Models column Exclude

2019-05-31 Thread Balaji Shetty
Dear Alexis Roda Sir

Thank You very Much.

definitely Race Condition may arise if we do not put code inn critical
section. But your suggested approach did not work and i found another
alternative which is working fine. If we put this code with necessary care
than will it be ok?



On Tue, May 28, 2019 at 8:33 PM Alexis Roda <
alexis.roda.villalo...@gmail.com> wrote:

> No, it isn't. It's not thread safe.
>
> Changes in shared state in multi-threaded code may lead to race
> conditions[1].
>
> Depending on your usage it may be very unlikely, but under the right
> circumstances root may end up having some missing fields in the form or dgp
> may see fields that it is not intended to see.
>
> Either protect the critical section[2] with some kind of lock or, as I
> suggested in my previous answer, create non-shared (request local) state.
>
>
> [1] https://en.wikipedia.org/wiki/Race_condition
> [2] https://en.wikipedia.org/wiki/Critical_section
>
> --
> 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/CAMDYoXa16C5goSadFbSoXQg1-v2zh9vrq%2BAk%3Dkqbq1rk%3DsgSQw%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>


-- 


*Mr. Shetty Balaji S.Asst. ProfessorDepartment of Information Technology,*
*SGGS Institute of Engineering & Technology, Vishnupuri, Nanded.MH.India*
*Official: 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.
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/CAECSbOvKUK0H2mrt_S%2BhmbVmmeneQXXc%2BR9a%3DFGHE7XOVmOK-Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: SOLUTION FounD : How to restore Models original Properties - Problem in Models column Exclude

2019-05-28 Thread Alexis Roda
No, it isn't. It's not thread safe.

Changes in shared state in multi-threaded code may lead to race
conditions[1].

Depending on your usage it may be very unlikely, but under the right
circumstances root may end up having some missing fields in the form or dgp
may see fields that it is not intended to see.

Either protect the critical section[2] with some kind of lock or, as I
suggested in my previous answer, create non-shared (request local) state.


[1] https://en.wikipedia.org/wiki/Race_condition
[2] https://en.wikipedia.org/wiki/Critical_section

-- 
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/CAMDYoXa16C5goSadFbSoXQg1-v2zh9vrq%2BAk%3Dkqbq1rk%3DsgSQw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


SOLUTION FounD : How to restore Models original Properties - Problem in Models column Exclude

2019-05-28 Thread Balaji Shetty
This is the proper code to customize models column userwise.
Here we are sharing model among multiple user and depending upon right
access, it is visible

in admin.py

  normaluser_fields =
['user','CourtType','SuitType','CaseNumber','CaseYear','CaseAdvocate','CasePartyNames',
'CaseOppositePartyNames','CaseLastDate']
  superuser_fields =
['UserNext','UserNextDate','UserNextDepartment','CaseStatus']
  inward_fields =
['InwardNumber','CaseNoticeDate','CaseNoticeSubject','CaseReceiverName','CaseInwardDate','CaseActionTaken']


  def get_form(self, request, obj=None, **kwargs):

username = request.user.username

print("- Inside get_exclude Method")
print (username)

if username == 'dgp': # if user is not a superuser

# if request.user.is_superuser:
# print("-  ***  Inside ROOT LOGIN ***---")

self.fields = self.normaluser_fields
elif username == 'inumber':
print("-  ***  Inside DGP LOGIN ***---")
self.fields = self.inward_fields
else:
self.fields = self.normaluser_fields + self.superuser_fields +
self.inward_fields


return super(ProfileAdmin, self).get_form(request, obj, **kwargs)



On Tue, May 28, 2019 at 1:02 PM Balaji Shetty 
wrote:

> Dear  Alexis Roda Sir
>
> Thank You very much for your nice reply. I check it.
>
> On Tue, May 28, 2019 at 1:55 AM Alexis Roda <
> alexis.roda.villalo...@gmail.com> wrote:
>
>> Not an admin expert here.
>>
>> You are modifying the modeladmin instance and, by the behavior you're
>> describing, it looks like it is a global (shared) object. I'm guessing, you
>> can confirm this by printing id(self) in add_view(). I'll bet you get
>> the same value in each request, so the instance is shared, so the change is
>> visible (persisted) across request boundaries.
>>
>> You may think about adding self.exclude = () in the else branch in order
>> to not exclude anything. Don't do that, you may end up leaking private
>> information to dgp if both root and dgp are logged in at the same time.
>>
>> Instead override the get_exclude method.
>>
>> See
>> https://docs.djangoproject.com/en/2.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_exclude
>>
>> --
>> 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/CAMDYoXbb42hpu3jAATXTwxPw9kO8XeZpLOczbNa-Bzw5QAdGuQ%40mail.gmail.com
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> --
>
>
> *Mr. Shetty Balaji S.Asst. ProfessorDepartment of Information Technology,*
> *SGGS Institute of Engineering & Technology, Vishnupuri, Nanded.MH.India*
> *Official: bsshe...@sggs.ac.in  *
> *  Mobile: +91-9270696267*
>
>

-- 


*Mr. Shetty Balaji S.Asst. ProfessorDepartment of Information Technology,*
*SGGS Institute of Engineering & Technology, Vishnupuri, Nanded.MH.India*
*Official: 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.
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/CAECSbOsDv%2Byx-D382S45y2%3DqfYo3FHxyUytqYDm%2B62DYkZopig%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.