def addDefaults(mapDict): # set the default information (mirroring forms.py as initial values) if 'region_name' not in mapDict or not mapDict['region_name']: mapDict['region_name'] = Region.objects.get(id = settings.XGDS_CURRENT_REGION_ID).name if 'number' not in mapDict or not mapDict['number']: mapDict['number'] = SAMPLE_MODEL.get().getCurrentNumber() # change the server time (UTC) to local time for display if 'collection_time' not in mapDict or not mapDict['collection_time']: utc_time = timezone.now() else: utc_time = mapDict['collection_time'] local_time = utcToLocalTime(utc_time) collection_time = local_time.strftime("%m/%d/%Y %H:%M:%S") mapDict['collection_time'] = collection_time return mapDict
def __init__(self, *args, **kwargs): super(SampleForm, self).__init__(*args, **kwargs) self.fields['pk'].initial = self.instance.pk if self.instance.collector: self.fields['collector_name'].initial = self.instance.collector.first_name + ' ' + self.instance.collector.last_name positionDict = self.instance.getPositionDict() self.fields['lat'].initial = positionDict['lat'] self.fields['lon'].initial = positionDict['lon'] if 'altitude' in positionDict: self.fields['altitude'].initial = positionDict['altitude'] self.fields['region'].initial = Region.objects.get(id = settings.XGDS_CURRENT_REGION_ID) self.fields['region'].empty_label = None # auto increment the sample number self.initial['number'] = self.instance.number if not self.instance.number: self.initial['number'] = SAMPLE_MODEL.get().getCurrentNumber() if self.instance.collection_time: utc_collection_time = self.instance.collection_time else: utc_collection_time = timezone.now() local_time = utcToLocalTime(utc_collection_time) collection_time = local_time.strftime("%m/%d/%Y %H:%M:%S") self.initial['collection_time'] = collection_time