Re: Please criticise use of global keyword in Admin

2022-05-18 Thread Michael Thomas
This has a pretty bad "smell", and I could imagine it being quite brittle
unless developers tip-toe around the code.

I'd suggest replacing this by implementing get_readonly_fields(), which
returns a fixed list based on the user type. It's much simpler, and much
more difficult to get wrong, in my opinion at least!

Kind Regards,
Michael Thomas

On Thu, May 19, 2022 at 9:34 AM Mike Dewhirst  wrote:

> The code below appears to work perfectly but it worries me because I
> have never used the global keyword before.
>
> Is there a generous guru who will criticise the code constructively for
> me please?
>
> The use case is Admin review of payment gateway receipt records.
>
> Users in the manager group can have readonly access to the transaction
> half of the information while all payment gateway cost info is hidden.
>
> Users in the consultant group get readonly access to the gateway info as
> well.
>
> Members of the operator group can do manual invoices so they need write
> access to a couple of fields.
>
> Many thanks
>
> Mike
>
> #admin.py for the billing app
>
> rec_fields = [
>  "company",
>  "chemical",
>  "invoice",
>  "currency",
>  "currency_amount",
>  "gst_amount",
>  "gst_rate",
>  "gst_name",
>  "amount_due",
>  "amount_paid",
>  "paid_by",
>  "date_paid",
> ]
>
>
> cons_fields = [
>  "gateway_flagfall",
>  "gateway_rate",
>  "gateway_amount",
>  "gateway_gst_amount",
>  "net_amount",
> ]
>
> receipt_fields = rec_fields[:]
> receipt_fields_ro = rec_fields[:]
> consultant_fields = cons_fields[:]
>
> def trim_fields():
>  global receipt_fields, receipt_fields_ro, consultant_fields
>  #<<
>  if "gateway_flagfall" in receipt_fields:
>  for field in consultant_fields:
>  receipt_fields.remove(field)
>  receipt_fields_ro = receipt_fields[:]
>
>
> def add_fields(operator=False):
>  global receipt_fields, receipt_fields_ro, consultant_fields
> #<<
>  if "gateway_flagfall" not in receipt_fields:
>  receipt_fields.extend(consultant_fields)
>  receipt_fields_ro.extend(consultant_fields)
>  if operator:
>  receipt_fields_ro.remove("amount_due")
>  receipt_fields_ro.remove("amount_paid")
>  receipt_fields_ro.remove("date_paid")
>
>
> class ReceiptAdmin(admin.ModelAdmin):
>
>  global receipt_fields, receipt_fields_ro  #
>
>  def has_add_permission(self, request, obj=None):
>  return is_operator(request.user)
>
>  def has_change_permission(self, request, obj=None):
>  return is_operator(request.user)
>
>  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
>  #
>  def get_queryset(self, request):
>  qs = super().get_queryset(request)
>  if qs:
>  if is_operator(request.user):
>  add_fields(operator=True)   #<
>  return qs
>  coy = get_user_company(request)
>  if coy:
>  qs = qs.filter(company_id=coy.id)
>  if is_consultant(request.user):
>  add_fields()#><
>  return qs
>  if is_manager(request.user):
>  trim_fields()   #><
>  return qs
>  #
>  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
>
>  model = Receipt
>  readonly_fields = receipt_fields_ro
>  fieldsets = (
>  (
>  "Detail for this Invoice/Receipt",
>  {
>  "fields": receipt_fields,
>  },
>  ),
>  )
>
>
> --
> Signed email is an absolute defence against phishing. This email has
> been signed with my private key. If you import my public key you can
> automatically decrypt my signature and be sure it came from me. Just
> ask and I'll send it to you. Your email software can handle signing.
>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a8fc7ec5-96a9-87f8-c3ba-f89312cdbad9%40dewhirst.com.au
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEdx1fp0kJuqsHdsPTTEeCNqtNXWsEOcaSSR16OVXVaEdBG6ew%40mail.gmail.com.


Re: Pytest coverage report 100%

2022-05-18 Thread Salima Begum
Hi,
Thank you for your response. We are basically writing a unit test for
Account_security function with this unit test code.


Unit test code :


> def test_Account_security_url(self):
> path = reverse('account-security')
> assert resolve(path).view_name == 'account-security'

-

Here is the function for which we write above unit test, but the test is
not covering these red colored statements. So, we are wondering what could
be the issue here.

>
> def Account_security(request):
> try:
> if ((request.session.get('email') is None) or
> (request.session.get('email') == "")):
> return HttpResponseRedirect("/home")
> dict_act = {}
> email = request.session.get('email')
> email = request.session.get('email')
> # save_url = save_current_url(request, email)
> # Based on logged in user email filtering list of objects.
> account = vk_Account_history.objects.filter(email=email)
> # Calculating logged in time.
> for i in account:
> some_date = diff_time_Acnt(i)
> dict_act[i.id] = some_date
> return render(request, 'account_security.html', {'account':
> account, 'fullname': fullname(email),
> some_date = diff_time_Acnt(i)
> dict_act[i.id] = some_date
> return render(request, 'account_security.html', {'account':
> account, 'fullname': fullname(email),
>  'name':
> first_last_initial(email),
>  'time':
> settings.SESSION_IDLE_TIMEOUT,
>  'some_date':
> dict_act,
>  })
> except Exception as e:
> logging.error(e)
> return render(request, 'account_security.html', {'account':
> account, 'fullname': fullname(email),
> logging.error(e)
> return render(request, 'account_security.html', {'account':
> account, 'fullname': fullname(email),
>  'name':
> first_last_initial(email),
>  'time':
> settings.SESSION_IDLE_TIMEOUT,
>  'some_date':
> dict_act,
>  })




Likewise it's happening in other tests also.

Thank you
~Salima

On Thu, May 19, 2022 at 2:24 AM Kasper Laudrup 
wrote:

> On 18/05/2022 09.20, Salima Begum wrote:
> > How to write a test case for total function without missing any
> > statement. Please help me out how to write a test case for above
> > function.
> >
>
> You inject values in your test case to make sure all branches are covered.
>
> Sometimes that requires a bit more trickery with mocking out some things
> but in the case of the function you've shown it should be fairly simple
> to achieve 100% test coverage, at least from the point of a coverage tool.
>
> Any specific parts you're having issues with? I can't help to notice
> that you return the exact same thing to the use in the case of any
> exception but only log the exception. That seems wrong but I'm not sure
> if that's related to your question.
>
> Kind regards,
> Kasper Laudrup
>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5e56c8b0-e5f6-9c9b-a9fd-77b5eedcd0d1%40stacktrace.dk
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMSz6bnhpcwNJeWtVu%3DNvEpSTVYe2s8bU6Ww4_FH6JgUjiCaag%40mail.gmail.com.


Please criticise use of global keyword in Admin

2022-05-18 Thread Mike Dewhirst
The code below appears to work perfectly but it worries me because I 
have never used the global keyword before.


Is there a generous guru who will criticise the code constructively for 
me please?


The use case is Admin review of payment gateway receipt records.

Users in the manager group can have readonly access to the transaction 
half of the information while all payment gateway cost info is hidden.


Users in the consultant group get readonly access to the gateway info as 
well.


Members of the operator group can do manual invoices so they need write 
access to a couple of fields.


Many thanks

Mike

#admin.py for the billing app

rec_fields = [
"company",
"chemical",
"invoice",
"currency",
"currency_amount",
"gst_amount",
"gst_rate",
"gst_name",
"amount_due",
"amount_paid",
"paid_by",
"date_paid",
]


cons_fields = [
"gateway_flagfall",
"gateway_rate",
"gateway_amount",
"gateway_gst_amount",
"net_amount",
]

receipt_fields = rec_fields[:]
receipt_fields_ro = rec_fields[:]
consultant_fields = cons_fields[:]

def trim_fields():
global receipt_fields, receipt_fields_ro, consultant_fields  #<<
if "gateway_flagfall" in receipt_fields:
for field in consultant_fields:
receipt_fields.remove(field)
receipt_fields_ro = receipt_fields[:]


def add_fields(operator=False):
global receipt_fields, receipt_fields_ro, consultant_fields  #<<
if "gateway_flagfall" not in receipt_fields:
receipt_fields.extend(consultant_fields)
receipt_fields_ro.extend(consultant_fields)
if operator:
receipt_fields_ro.remove("amount_due")
receipt_fields_ro.remove("amount_paid")
receipt_fields_ro.remove("date_paid")


class ReceiptAdmin(admin.ModelAdmin):

global receipt_fields, receipt_fields_ro  #

def has_add_permission(self, request, obj=None):
return is_operator(request.user)

def has_change_permission(self, request, obj=None):
return is_operator(request.user)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
def get_queryset(self, request):
qs = super().get_queryset(request)
if qs:
if is_operator(request.user):
add_fields(operator=True)   #<
return qs
coy = get_user_company(request)
if coy:
qs = qs.filter(company_id=coy.id)
if is_consultant(request.user):
add_fields()#><
return qs
if is_manager(request.user):
trim_fields()   #><
return qs
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

model = Receipt
readonly_fields = receipt_fields_ro
fieldsets = (
(
"Detail for this Invoice/Receipt",
{
"fields": receipt_fields,
},
),
)


--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.

--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a8fc7ec5-96a9-87f8-c3ba-f89312cdbad9%40dewhirst.com.au.


OpenPGP_signature
Description: OpenPGP digital signature


Re: PHP website | a way to refresh the page without reloading

2022-05-18 Thread Mike Dewhirst

On 19/05/2022 4:23 am, Dias André wrote:
Good afternoon! I'm new to programming and I'm making a website in 
php, I would like to know if there is a way for my website to update 
without reloading the page and always going to the top of it.

sry for the english


When I was new to web programming I didn't know which way to to turn.

I had come from Delphi and really wanted an object oriented approach. I 
tried Perl with its blessed hashes and then saw PHP was object oriented 
so I looked at that. Unfortunately php was only partly object oriented 
and I was not mentally sufficiently rigorous to use it properly. My 
mistakes lay hidden waiting to bite me. In my opinion you have to be a 
really brilliant programmer to use php. I'm not and never will be that 
brilliant so I cannot use php.


Then I found Python and it was perfect. It was originally designed as a 
teaching language for schoolkids. Just my cup of tea. Easy to learn and 
built-in guardrails.


Python didn't let me make the sort of mistakes php almost forced on me. 
It is completely object oriented.


Then I looked for a web framework and back then there were many. I even 
went out on the Python lists and asked what people thought. There were 
too many opinions. It would take me forever to learn how to use all 
these different frameworks just so I could pick one.


I decided to write my own just to teach myself Python.

But I needed a DBMS. MySQL was big at the time and I was actually using 
it in one application. A friend told it was not the best and I should 
use PostgreSQL. But because I was starting from scratch I figured I 
would write a simple text-file backend database until I was ready to 
decide which way to to turn.


Then the Python community declared Django was going to be its favourite 
web framework. So I decided on Django. The Django docs seemed to favour 
PostgreSQL so apart from using Apache because I was already familiar 
with it, that was all my decisions made.


It took a long time for me to settle and you have apparently come to 
Django by pure luck :-)


BTW, you probably need to look at javascript or HTMX for updating parts 
of a page without reloading.


Cheers and welcome!

Mike



Dias André,
greetings.
--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/eef9e2f0-ebce-403e-82e3-334d7575e78fn%40googlegroups.com 
.



--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.

--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/09cb6eaf-b34b-32f8-9857-97c4ccff15cb%40dewhirst.com.au.


OpenPGP_signature
Description: OpenPGP digital signature


PHP website | a way to refresh the page without reloading

2022-05-18 Thread Dias André
Good afternoon! I'm new to programming and I'm making a website in php, I 
would like to know if there is a way for my website to update without 
reloading the page and always going to the top of it.
sry for the english

Dias André,
greetings.

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/eef9e2f0-ebce-403e-82e3-334d7575e78fn%40googlegroups.com.


Re: Image Update not Working in Production, Please Help

2022-05-18 Thread Kasper Laudrup

On 15/05/2022 03.15, Wisdom Ugochukwu wrote:
I hosted a Django project on Name Cheap, it works well on development, 
but on going live I noticed image creation and update is not working

here is the error I'm getting! Please kindly help


/lib64/libc.so.6: version `GLIBC_2.14' not found (required by 
/home/aiteglbu/virtualenv/humanitiesmed/3.8/lib/python3.8/site-packages/PIL/_imaging.cpython-38-x86_64-linux-gnu.so
  )



Seems like you've somehow managed to use a version of PIL compiled 
against another version of glibc than the one running on your live system.


Hard to guess how you've managed to do that without any details but I 
might assume that you've simply copied you're virtual environment from 
development (running another, incompatiable glibc version) to production.


That's not the way to do it. Keep your requirements in a text file 
(requirements.txt) and setup a fresh virtual environment on your 
production system instead installing the requirements from that file.


Kind regards,
Kasper Laudrup

--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1535e8c4-553d-60ae-69e9-9c704a559d21%40stacktrace.dk.


OpenPGP_0xE5D9CAC64AAA55EB.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: Django deploy Apache

2022-05-18 Thread Kasper Laudrup

On 13/05/2022 13.56, Nicolas Passarini wrote:
Hello, i ve deployed a django app in apache. the problem is that i works 
for 5 minutes and then it stops responding.




That sucks. Sounds like you have some issues you need to look into.

Good luck.

Kind regards,
Kasper Laudrup



--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/84858a53-2256-4bee-889a-ff365127f86en%40googlegroups.com 
.


--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/21b9a5fd-873e-503b-5f87-d67e60ce2faf%40stacktrace.dk.


OpenPGP_0xE5D9CAC64AAA55EB.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: Pytest coverage report 100%

2022-05-18 Thread Kasper Laudrup

On 18/05/2022 09.20, Salima Begum wrote:

How to write a test case for total function without missing any
statement. Please help me out how to write a test case for above
function.



You inject values in your test case to make sure all branches are covered.

Sometimes that requires a bit more trickery with mocking out some things 
but in the case of the function you've shown it should be fairly simple 
to achieve 100% test coverage, at least from the point of a coverage tool.


Any specific parts you're having issues with? I can't help to notice 
that you return the exact same thing to the use in the case of any 
exception but only log the exception. That seems wrong but I'm not sure 
if that's related to your question.


Kind regards,
Kasper Laudrup

--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5e56c8b0-e5f6-9c9b-a9fd-77b5eedcd0d1%40stacktrace.dk.


OpenPGP_0xE5D9CAC64AAA55EB.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: working of python with slack

2022-05-18 Thread Kasper Laudrup

On 18/05/2022 14.05, Abhinandan K wrote:
Is anybody know how to run python script with slack...for example run 
the scrapped data from fiplkart bot working on slackis anybody have 
answer reply as soon as possible




How is this is any way related to Django?

Kind regards,

Kasper Laudrup

--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/bc0a34c7-21a6-38e1-7b58-b6d2998422c9%40stacktrace.dk.


OpenPGP_0xE5D9CAC64AAA55EB.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


working of python with slack

2022-05-18 Thread Abhinandan K
Is anybody know how to run python script with slack...for example run the
scrapped data from fiplkart bot working on slackis anybody have answer
reply as soon as possible

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAA6_Mp57Q%3D-K3b1EXfVFmYgjTOrpGWzpgd0G0Qy0qdjkYxSA7w%40mail.gmail.com.


Pytest coverage report 100%

2022-05-18 Thread Salima Begum
Hi all,
How to write a pytest case to get coverage report to 100%.

```
def Account_security(request):
try:
if ((request.session.get('email') is None) or
(request.session.get('email') == "")):
return HttpResponseRedirect("/home")
dict_act = {}
email = request.session.get('email')
# save_url = save_current_url(request, email)
# Based on logged in user email filtering list of objects.
account = Account_history.objects.filter(email=email)
# Calculating logged in time.
for i in account:
some_date = diff_time_Acnt(i)
dict_act[i.id] = some_date
return render(request, 'account_security.html',
  {'account': account,

   'fullname': fullname(email),
'name': first_last_initial(email),
'time': settings.SESSION_IDLE_TIMEOUT,
 'some_date': dict_act,
 })
except Exception as e:
logging.error(e)
return render(request, 'account_security.html', {'account':
account, 'fullname': fullname(email),
 'name':
first_last_initial(email),
 'time':
settings.SESSION_IDLE_TIMEOUT,
 'some_date': dict_act,
 })
```

How to write a test case for total function without missing any
statement. Please help me out how to write a test case for above
function.

Thanks
~Salima

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMSz6bnqJs1fgMCUD1FwhzZS3U0aqW0sw1aK4B757viuoM7RbA%40mail.gmail.com.