def clean(self): cleaned_data = super(OrderForm, self).clean() method = self.cleaned_data.get('method') location = self.site.location_set.all()[0] if method == Order.METHOD_DELIVERY: address_fields = ['street', 'city', 'state', 'zip'] if not all(cleaned_data.get(field) for field in address_fields): msg = 'You must enter an address for delivery.' raise forms.ValidationError(msg) address = ' '.join( cleaned_data.get(field) for field in address_fields) msg = """We apologize, but it appears you are outside of our delivery area. Please choose one of the other options or call us at %s.""" % location.phone try: lon, lat = geocode(address) except: raise forms.ValidationError(msg) point = Point(lon, lat) area = location.delivery_area if not area.contains(point): raise forms.ValidationError(msg) lead_time = location.delivery_lead_time else: lead_time = location.lead_time ready_by = self.cleaned_data.get('ready_by') method_display = dict(Order.METHOD_CHOICES).get(method) if ready_by and method_display: # check that there is not a date discrepancy across time zones loc_zone = self.location.get_timezone() day = self.cleaned_data.get('day') if day is None: day = timezone(settings.TIME_ZONE).localize( datetime.now()).astimezone(loc_zone).date() else: if location.schedule.is_open( location, loc_zone.localize(datetime.combine( day, ready_by))) != TIME_OPEN: raise forms.ValidationError( "We won't be open at %s on %s." % (ready_by.strftime("%H:%M%P"), day.strftime("%b %d, %Y"))) ready_by = loc_zone.localize(datetime.combine(day, ready_by)) server_tz = timezone(settings.TIME_ZONE) if ready_by < server_tz.localize(datetime.now() + timedelta(minutes=lead_time)): raise forms.ValidationError( '%s orders must be placed %d minutes in advance.' % (method_display, lead_time)) self.ready_by = ready_by return cleaned_data
def save(self, *args, **kwargs): new = not self.id if new and self.schedule is None: self.schedule = self.site.schedule_set.get(master=True) if self.address and not (self.lon and self.lat): try: self.lon, self.lat = [str(f) for f in geocode(self.address)] except GeocodeError: pass super(Location, self).save(*args, **kwargs) if new: from tiger.core.models import LocationStockInfo for item in self.site.item_set.all(): LocationStockInfo.objects.create(location=self, item=item) KeyChain.footer_locations.invalidate(self.site.id) KeyChain.sidebar_locations.invalidate(self.site.id)
def clean(self): cleaned_data = super(OrderForm, self).clean() method = self.cleaned_data.get("method") location = self.site.location_set.all()[0] if method == Order.METHOD_DELIVERY: address_fields = ["street", "city", "state", "zip"] if not all(cleaned_data.get(field) for field in address_fields): msg = "You must enter an address for delivery." raise forms.ValidationError(msg) address = " ".join(cleaned_data.get(field) for field in address_fields) msg = ( """We apologize, but it appears you are outside of our delivery area. Please choose one of the other options or call us at %s.""" % location.phone ) try: lon, lat = geocode(address) except: raise forms.ValidationError(msg) point = Point(lon, lat) area = location.delivery_area if not area.contains(point): raise forms.ValidationError(msg) lead_time = location.delivery_lead_time else: lead_time = location.lead_time ready_by = self.cleaned_data.get("ready_by") method_display = dict(Order.METHOD_CHOICES).get(method) if ready_by and method_display: # check that there is not a date discrepancy across time zones loc_zone = self.location.get_timezone() day = self.cleaned_data.get("day") if day is None: day = timezone(settings.TIME_ZONE).localize(datetime.now()).astimezone(loc_zone).date() else: if location.schedule.is_open(location, loc_zone.localize(datetime.combine(day, ready_by))) != TIME_OPEN: raise forms.ValidationError( "We won't be open at %s on %s." % (ready_by.strftime("%H:%M%P"), day.strftime("%b %d, %Y")) ) ready_by = loc_zone.localize(datetime.combine(day, ready_by)) server_tz = timezone(settings.TIME_ZONE) if ready_by < server_tz.localize(datetime.now() + timedelta(minutes=lead_time)): raise forms.ValidationError( "%s orders must be placed %d minutes in advance." % (method_display, lead_time) ) self.ready_by = ready_by return cleaned_data