def add_location(self, latitude, longitude, lot_type, address=None, pictures=None, description=None): """ Adds a new Location object to the database Returns the newly created Location object """ new_location = Location() new_location.latitude = latitude new_location.longitude = longitude new_location.address = address new_location.lot_type = lot_type new_location.description = description # Validate the picture to make sure it's not a malicious file try: if pictures: i = 1 for picture in pictures[:3]: setattr(new_location, "picture%i" % i, ImageField().clean(picture)) i += 1 except ValidationError as ve: e = Exception(self) e.message = '; '.join(ve.messages) raise e new_location.save() return new_location