Login

DateTimeFluxCapacitor

Author:
jbcurtin
Posted:
January 29, 2011
Language:
Python
Version:
1.2
Score:
0 (after 0 ratings)

Takes the current DateTime and adds either days, hours, minutes, or seconds to the datetime object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class DateTimeFluxCapacitor(models.DateTimeField):
	def __init__(self,verbose_name = None, name = None, flux_day = 0, flux_hour = 0, flux_minute = 0, flux_second = 0 , **kwargs):
		self.flux_day , self.flux_hour , self.flux_minute , self.flux_second = flux_day , flux_hour , flux_minute , flux_second
		if self.flux_day or self.flux_hour or self.flux_minute or self.flux_second:
			kwargs['blank'] = True
			kwargs['editable'] = False
		super(DateTimeFluxCapacitor,self).__init__(verbose_name,name,False,False)

	def pre_save(self,model_instance,add):
		if self.flux_day or self.flux_hour or self.flux_minute or self.flux_second:
			value = datetime.datetime.now() + datetime.timedelta(days = self.flux_day , hours = self.flux_hour , minutes = self.flux_minute , seconds = self.flux_second )
			setattr(model_instance,self.attname,value)
			return value
		else:
			return super(DateTimeFluxCapacitor,self).pre_save(model_instance,add)
	def get_internal_type(self):
		return "DateTimeField"

More like this

  1. Django Settings Assignment Expressions aka Walrus Operator example by webology 2 weeks, 5 days ago
  2. codigo alto nivel by MrRocklion 2 months, 2 weeks ago
  3. Load template from specific app by Krzysiek555 3 months ago
  4. PostgreSQL JSON subqueries by dolamroth 3 months, 1 week ago
  5. "Magic Link" Management Command by webology 8 months, 1 week ago

Comments

Please login first before commenting.