Re: Weird date math bug?

2017-09-03 Thread Lachlan Musicman
On 1 September 2017 at 16:13, Melvyn Sopacua  wrote:

> A few tips on your code:
>
> def get_latest_chemo(self):
> chemo = ChemoRegime.objects.filter(pat
> ient=self).latest('stop_date')
>
> class ChemoRegime(models.Model):
> patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
>
> Use the related manager here. To make it readable, use:
>
> class ChemoRegime(models.Model):
> patient = models.ForeignKey(Patient, on_delete=models.CASCADE,
> related_name='chemo_regimes')
>
> class Patient(models.Model):
> def get_latest_chemo(self):
> return self.chemo_regimes.latest('stop_date')
>
> When providing properties for use in templates, make them properties -
> makes the template more readable:
>
> @property
> def latest_chemo(self):
>  return self.chemo_regimes.latest('stop_date')
>
> {{ patient.latest_chemo }}
>
> Similarly you can abstract active regimes:
>
> class ChemoRegime(models.Model):
>  @property
>  def is_active(self):
>   return self.stop_date is None
>
> {% if chemo.is_active %}Current{% endif %}
>
>
> Of course, latest_chemo, should really be this if
> active and non-active regimes can be mixed:
>
> @property
> def has_active_chemo(self):
>  return self.chemo_regimes.filter(stop_date__isnull=True).count()
> > 0
>
> @property
> def latest_chemo(self):
>  if not self.chemo_regimes.count():
>   return None
>  if self.has_active_chemo:
>   return self.chemo_regimes.latest('start_date')
>  return self.chemo_regimes.latest('stop_date')
>
> Hope this helps :)
>


Melvyn - have only just got around to implementing your suggestions.
Excellent, worked a treat. Thank you for the new tricks!

Cheers
L.







--
"The antidote to apocalypticism is *apocalyptic civics*. Apocalyptic civics
is the insistence that we cannot ignore the truth, nor should we panic
about it. It is a shared consciousness that our institutions have failed
and our ecosystem is collapsing, yet we are still here — and we are
creative agents who can shape our destinies. Apocalyptic civics is the
conviction that the only way out is through, and the only way through is
together. "

*Greg Bloom* @greggish https://twitter.com/greggish/
status/873177525903609857

-- 
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/CAGBeqiOt%3DQGLWfxYxeYW2ajogpoSoZQLib3r727Bvsx3FB6AEg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Weird date math bug?

2017-09-01 Thread Lachlan Musicman
On 1 September 2017 at 16:37, James Schneider 
wrote:

> Also, if you are developing this application for use within the USA, be
> sure that you are not capturing or storing data that would fall under the
> HIPAA definition of personal medical data. If it does, your legal
> compliance responsibilities and liabilities will skyrocket.
>
> I'm not a lawyer, so I can't say for sure whether or not these data fields
> would be considered under HIPAA, but a compromise of medical data is
> extremely expensive, and I believe you can be held personally liable in
> some cases, even if developing for an employer.
>
> https://www.hhs.gov/hipaa/for-professionals/index.html
>
> If not in the US, there may be other regulations.
>

We are not in the US, but our laws are equally as strict. The culture isn't
quite so litigious either.

Having said that, all "patients" will only have an id, and the id->name
will be mapped on paper in a filing cabinet. On top of that, it's within
the corporate firewall. Which isn't perfect, I'm sure, but it pretty good.
You need to have access to the staff LAN (via eth) to access the site.

This is the first in a long "project to replace all access 2003/2007/2010
dbs" with something more...contemporary.

cheers
L.




--
"The antidote to apocalypticism is *apocalyptic civics*. Apocalyptic civics
is the insistence that we cannot ignore the truth, nor should we panic
about it. It is a shared consciousness that our institutions have failed
and our ecosystem is collapsing, yet we are still here — and we are
creative agents who can shape our destinies. Apocalyptic civics is the
conviction that the only way out is through, and the only way through is
together. "

*Greg Bloom* @greggish
https://twitter.com/greggish/status/873177525903609857

-- 
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/CAGBeqiOv%3D%3DpcKai8dUqTgKw7g7ZbJkVXAsZYL21GLaOSorCb2w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Weird date math bug?

2017-09-01 Thread James Schneider
Also, if you are developing this application for use within the USA, be
sure that you are not capturing or storing data that would fall under the
HIPAA definition of personal medical data. If it does, your legal
compliance responsibilities and liabilities will skyrocket.

I'm not a lawyer, so I can't say for sure whether or not these data fields
would be considered under HIPAA, but a compromise of medical data is
extremely expensive, and I believe you can be held personally liable in
some cases, even if developing for an employer.

https://www.hhs.gov/hipaa/for-professionals/index.html

If not in the US, there may be other regulations.

-James

-- 
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/CA%2Be%2BciViwO0%2BkQh2sxRvapTxR8S-0P24s0CNFibNjT85pzKugg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Weird date math bug?

2017-09-01 Thread Melvyn Sopacua
A few tips on your code:

def get_latest_chemo(self):
chemo = ChemoRegime.objects.filter(patient=self).latest('stop_date')

class ChemoRegime(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)

Use the related manager here. To make it readable, use:

class ChemoRegime(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE,
related_name='chemo_regimes')

class Patient(models.Model):
def get_latest_chemo(self):
return self.chemo_regimes.latest('stop_date')

When providing properties for use in templates, make them properties -
makes the template more readable:

@property
def latest_chemo(self):
 return self.chemo_regimes.latest('stop_date')

{{ patient.latest_chemo }}

Similarly you can abstract active regimes:

class ChemoRegime(models.Model):
 @property
 def is_active(self):
  return self.stop_date is None

{% if chemo.is_active %}Current{% endif %}


Of course, latest_chemo, should really be this if
active and non-active regimes can be mixed:

@property
def has_active_chemo(self):
 return self.chemo_regimes.filter(stop_date__isnull=True).count() > 0

@property
def latest_chemo(self):
 if not self.chemo_regimes.count():
  return None
 if self.has_active_chemo:
  return self.chemo_regimes.latest('start_date')
 return self.chemo_regimes.latest('stop_date')

Hope this helps :)

On Fri, Sep 1, 2017 at 7:22 AM, Lachlan Musicman  wrote:
> On 1 September 2017 at 14:57, James Schneider 
> wrote:
>>
>> I'm guessing that ChemoRegime.regime_age() contains line 95.
>>
>> ChemoRegime defines the stop_date field with null=True and blank=True. I'm
>> guessing this is because patients may not have yet stopped treatment, so
>> they have no stop_date.
>>
>> With that in mind, self.stop_date may be None in Python (and Null in the
>> DB). Subtracting None from a real datetime.date object is not supported.
>>
>> Ensure that stop_date has a date value before doing date math with it.
>>
>> -James
>>
>
> I am so embarrassed. Thank you James, spot on.
>
> cheers
> L.
>
> --
> 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/CAGBeqiM-kY1br4bzb5yWVEOCnXhcYd3ZoV8bUey1nJoS1iNWuQ%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



-- 
Melvyn Sopacua

-- 
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/CA%2Bgw1GUaMHkuwG9YU-G_7fLo3vWt-5xd7BW_%3DwwOYL_qeUGJiA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Weird date math bug?

2017-08-31 Thread James Schneider
>
I am so embarrassed. Thank you James, spot on.

cheers
L.

No worries. I've stuck my foot in my mouth more than once on this list.

-James

-- 
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/CA%2Be%2BciW3mzcCq%3DhQPOHL%2B6f3nOKU%3D%2B8vKjiAXDe8Sq1HW2QCzw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Weird date math bug?

2017-08-31 Thread Lachlan Musicman
On 1 September 2017 at 14:57, James Schneider 
wrote:

> I'm guessing that ChemoRegime.regime_age() contains line 95.
>
> ChemoRegime defines the stop_date field with null=True and blank=True. I'm
> guessing this is because patients may not have yet stopped treatment, so
> they have no stop_date.
>
> With that in mind, self.stop_date may be None in Python (and Null in the
> DB). Subtracting None from a real datetime.date object is not supported.
>
> Ensure that stop_date has a date value before doing date math with it.
>
> -James
>
>
I am so embarrassed. Thank you James, spot on.

cheers
L.

-- 
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/CAGBeqiM-kY1br4bzb5yWVEOCnXhcYd3ZoV8bUey1nJoS1iNWuQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Weird date math bug?

2017-08-31 Thread James Schneider
On Aug 31, 2017 9:44 PM, "Lachlan Musicman"  wrote:

Sorry, let me start again:


Yay, I'm not the only one that does it! Lol...





class ChemoRegime(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
chemotype = models.ForeignKey(ChemoType, on_delete=models.CASCADE)
start_date = models.DateField(null=True, blank=True)
stop_date = models.DateField(null=True, blank=True)

def regime_age(self):
age = timezone.now().date() - self.stop_date
return age.days






But in the browser:

Exception Type: TypeError
Exception Value:

unsupported operand type(s) for -: 'datetime.date' and 'NoneType'

Exception Location: ./patients/models.py in regime_age, line 95


line 95 is age = timezone.now().date() - self.stop_date


What am I doing wrong?

cheers
L.


I'm guessing that ChemoRegime.regime_age() contains line 95.

ChemoRegime defines the stop_date field with null=True and blank=True. I'm
guessing this is because patients may not have yet stopped treatment, so
they have no stop_date.

With that in mind, self.stop_date may be None in Python (and Null in the
DB). Subtracting None from a real datetime.date object is not supported.

Ensure that stop_date has a date value before doing date math with it.

-James

-- 
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/CA%2Be%2BciXMOY5Mx5C91e0sXasXKa-f5mCga%2BDtf29AtQDaL1sqUg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Weird date math bug?

2017-08-31 Thread Lachlan Musicman
Sorry, let me start again:


Is there a weird date math bug in Django 1.11.4? I say weird because surely
not?

I can't see any tickets in the tracker, but I'm definitely seeing it in
production.

Using postgresql:


models.py

class ChemoType(models.Model):
name = models.CharField(max_length=50, unique=True)

class Patient(models.Model):
chemo_regimes = models.ManyToManyField(
ChemoType,
through='ChemoRegime',
)
def get_latest_chemo(self):
chemo = ChemoRegime.objects.filter(patient=self).latest('stop_date')
return chemo

def get_latest_chemo_age(self):
chemo = ChemoRegime.objects.filter(patient=self).latest('stop_date')
return chemo.regime_age()

class ChemoRegime(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
chemotype = models.ForeignKey(ChemoType, on_delete=models.CASCADE)
start_date = models.DateField(null=True, blank=True)
stop_date = models.DateField(null=True, blank=True)

def regime_age(self):
age = timezone.now().date() - self.stop_date
return age.days




In django_extension's shell plus:

>>> p = Patient.objects.get(id=1)
>>> p.get_latest_chemo()

>>> p.get_latest_chemo_age()
12
>>> p.get_latest_chemo_age
>
>>> type(p.get_latest_chemo_age())





In the template

  {% for patient in object_list %}

   {{ patient
}}
   {{ patient.get_latest_chemo_age }} ago





But in the browser:

Exception Type: TypeError
Exception Value:

unsupported operand type(s) for -: 'datetime.date' and 'NoneType'

Exception Location: ./patients/models.py in regime_age, line 95


line 95 is age = timezone.now().date() - self.stop_date


What am I doing wrong?

cheers
L.

--
"The antidote to apocalypticism is apocalyptic civics. Apocalyptic civics
is the insistence that we cannot ignore the truth, nor should we panic
about it. It is a shared consciousness that our institutions have failed
and our ecosystem is collapsing, yet we are still here — and we are
creative agents who can shape our destinies. Apocalyptic civics is the
conviction that the only way out is through, and the only way through is
together. "

Greg Bloom @greggish https://twitter.com/greggish/status/873177525903609857

-- 
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/CAGBeqiMoZXiwUhL-gL1uPzOw%3DC4JfoDZnfOFs6Qys4HU7RZV%2BQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Weird date math bug?

2017-08-31 Thread Lachlan Musicman
Is there a weird date math bug in Django 1.11.4? I say weird because surely
not?

I can't see any tickets in the tracker, but I'm definitely seeing it in
production.

Using postgresql:


class Patient(models.Model):


--
"The antidote to apocalypticism is *apocalyptic civics*. Apocalyptic civics
is the insistence that we cannot ignore the truth, nor should we panic
about it. It is a shared consciousness that our institutions have failed
and our ecosystem is collapsing, yet we are still here — and we are
creative agents who can shape our destinies. Apocalyptic civics is the
conviction that the only way out is through, and the only way through is
together. "

*Greg Bloom* @greggish
https://twitter.com/greggish/status/873177525903609857

-- 
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/CAGBeqiMPy_7DU-DVrZYypp9aDAqRJS4dDbAt-33cy0MeUYLjFg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.