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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.