def generate_object_item_delivery(): idelivr = ItemDelivery() type_d = generate_type_driver() idelivr.load = generate_load_transport(type_d) idelivr.name = generate_name_item_delivery(type_d) # TODO: change to all BR, for a while, just CG. lat_o, lng_o = random_lat_lon() lat_t, lng_t = random_lat_lon() idelivr.lat = lat_o idelivr.lng = lng_o address = Address() address.lat = lat_t address.lng = lng_t idelivr.address = geolocation_reverse(lat_o, lng_o) address.address = geolocation_reverse(lat_t, lng_t) address.save() idelivr.target = address idelivr.save() for i in range(generate_stops_number()): lat_stop, lng_stop = random_lat_lon() stop = Stop() stop.lat = lat_stop stop.lng = lng_stop stop.item_delivery = idelivr stop.save() idelivr.distance = get_distance_total_idelivery(idelivr) idelivr.save() idelivr.amount_receivable = Money( generate_profit_item_delivery(type_d, idelivr.distance) + get_cost_travel(generate_kml_transport(type_d), idelivr.distance, type_d), 'BRL') idelivr.save() return idelivr
def post(self, request): """ create a new user with addresses """ form = UserAddress(request.POST) if form.is_valid(): try: address = Address() fk_user = User.objects.get(user=form.cleaned_data['user']) address.user = fk_user address.address = form.cleaned_data['address'] address.save() except User.DoesNotExist: User(user=form.cleaned_data['user']).save() address = Address() fk_user = User.objects.get(user=form.cleaned_data['user']) address.user = fk_user address.address = form.cleaned_data['address'] address.save() return redirect('/')
def save(): ''' Save a new or an existing organisation :return: User's organisations home page ''' form = OrganisationForm() if form.validate_on_submit(): # check if user is trying to edit id = request.form['id'] new = id == "-1" user_can_edit = not new and user_has_rights(id) if not new and not user_has_rights(id): error = "Not allowed to edit this organisation" return to_403(error) # Create/get organisation if new: organisation = Organisation( name=request.form['name'], manager_name=request.form['manager_name']) organisation.users.append(g.user) db.session.add(organisation) db.session.commit() else: organisation = Organisation.query.get_or_404(id) organisation.name = request.form['name'] organisation.manager_name = request.form['manager_name'] if new: address = Address(address=request.form['address'], city=request.form['city'], province=request.form['province'], country=request.form['country'], postal_code=request.form['postal_code'], organisation_id=organisation.id) db.session.add(address) else: address = organisation.addresses.first() address.address = request.form['address'] address.city = request.form['city'] address.province = request.form['province'] address.country = request.form['country'] address.postal_code = request.form['postal_code'] address.organisation_id = organisation.id db.session.commit() flash(organisation.name + " has been saved") return redirect(url_for('organisations.index'))