def test_get_company_contact(regular_user): person_contact = get_person_contact(regular_user) assert person_contact != AnonymousContact() assert not get_company_contact(regular_user) company_contact = create_random_company() company_contact.members.add(person_contact) assert get_company_contact(regular_user) == company_contact
def test_company_edit_form_links_company(regular_user, rf): get_default_shop() person = get_person_contact(regular_user) assert not get_company_contact(regular_user) client = SmartClient() client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) company_edit_url = reverse("shoop:company_edit") soup = client.soup(company_edit_url) data = default_company_data() data.update(default_address_data("billing")) data.update(default_address_data("shipping")) response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 302 assert get_company_contact(regular_user)
def get_form(self, form_class): contact = get_company_contact(self.request.user) form_group = FormGroup(**self.get_form_kwargs()) form_group.add_form_def( "billing", AddressForm, kwargs={"instance": contact.default_billing_address if contact else None} ) form_group.add_form_def( "shipping", AddressForm, kwargs={"instance": contact.default_shipping_address if contact else None} ) form_group.add_form_def("contact", CompanyContactForm, kwargs={"instance": contact}) return form_group
def test_customer_company_member(regular_user): get_default_shop() # Create a shop mw = ShoopFrontMiddleware() request = get_unprocessed_request() request.user = regular_user person = get_person_contact(regular_user) company = create_random_company() company.members.add(person) assert get_company_contact(regular_user) == company mw.process_request(request) check_request_attribute_basics(request) assert isinstance(request.person, PersonContact) assert isinstance(request.customer, CompanyContact) company = get_company_contact(request.user) assert company and (company == request.customer)
def test_company_still_linked_if_customer_contact_edited(regular_user): get_default_shop() person = get_person_contact(regular_user) assert not get_company_contact(regular_user) company = CompanyContact() company.save() company.members.add(person) assert get_company_contact(regular_user) client = SmartClient() client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) customer_edit_url = reverse("shoop:customer_edit") soup = client.soup(customer_edit_url) data = default_customer_data() data.update(default_address_data("billing")) data.update(default_address_data("shipping")) response, soup = client.response_and_soup(customer_edit_url, data, "post") assert response.status_code == 302 assert get_company_contact(regular_user)
def form_valid(self, form): contact = form["contact"].save() user = contact.user billing_address = form["billing"].save() shipping_address = form["shipping"].save() if billing_address.pk != contact.default_billing_address_id: # Identity changed due to immutability contact.default_billing_address = billing_address if shipping_address.pk != contact.default_shipping_address_id: # Identity changed due to immutability contact.default_shipping_address = shipping_address if not bool(get_company_contact(self.request.user)): # Only update user details for non-company members user.email = contact.email user.first_name = contact.first_name user.last_name = contact.last_name user.save() contact.save() messages.success(self.request, _("Account information saved successfully.")) return redirect("shoop:customer_edit")
def _set_customer(self, request): company = get_company_contact(request.user) request.customer = (company or request.person) request.is_company_member = bool(company)