def address_edit(request, address_id=None): address = Address.get_or_none(address_id) if address is None and address_id is not None: raise Http404(_("Address not found")) if (address is not None and address.organization != request.user.profile.organization): return PermissionDenied() context = {"address": address} form = AddressForm(client=request.user.profile, instance=address) if request.method == "POST": form = AddressForm(request.POST, instance=address, client=request.user.profile) if form.is_valid(): try: res = form.save() except Exception as exp: import traceback print(traceback.format_exc()) logger.error(exp) messages.error(request, _("Error while saving… %(err)s") % {"err": exp}) else: messages.success(request, form.success_message(res, not bool(address))) return redirect("address_list") else: pass context["form"] = form return render(request, "address_edit.html", context)
def address_delete(request, address_id=None): address = Address.get_or_none(address_id) if address is None: raise Http404(_("Address not found")) if address.organization != request.user.profile.organization: return PermissionDenied() try: address.delete() messages.success( request, _("Successfuly deleted Address <em>%(address)s</em>") % {"address": address}, ) except Exception as exp: logger.error(f"Unable to delete Address {address.id}: {exp}") messages.error( request, _("Unable to delete Address <em>%(address)s</em>: -- ref %(err)s") % { "address": address, "err": exp }, ) return redirect("address_list")
def clean_address(self): if self.cleaned_data.get("address", "none") == "none": return address = Address.get_or_none(self.cleaned_data.get("address")) if address and address.organization != self.organization: raise forms.ValidationError(_("Not your address"), code="invalid") return address
def __init__(self, *args, **kwargs): client = kwargs.pop("client") super().__init__(*args, **kwargs) self.client = client self.organization = client.organization self.fields["config"].choices = Configuration.get_choices( self.organization) self.fields["address"].choices = Address.get_choices(self.organization) self.fields["media"].choices = Media.get_choices()
def clean_phone(self): if not self.cleaned_data.get("phone"): return self.cleaned_data.get("phone") try: cleaned_phone = Address.cleaned_phone( self.cleaned_data.get("phone")) except Exception as exp: logger.error(exp) logger.exception(exp) raise forms.ValidationError("Invalid Phone Number", code="invalid") return cleaned_phone
def __init__(self, *args, **kwargs): request = kwargs.pop("request") super().__init__(*args, **kwargs) self.client = request.user.profile self.request_lang = get_language_from_request(request) self.organization = self.client.organization self.fields["config"].choices = Configuration.get_choices( self.organization) self.fields["address"].choices = [ ("none", "Myself") ] + Address.get_choices(self.organization) self.fields["media"].choices = Media.get_choices( kind=None if self.client.can_order_physical else Media.VIRTUAL, display_units=self.client.is_limited, ) self.fields["kind"].choices = filter( lambda item: self.client.can_order_physical or item[0] != Media. PHYSICAL, self.KIND_CHOICES.items(), )
def __init__(self, *args, **kwargs): client = kwargs.pop("client") print(client, client.is_limited) super().__init__(*args, **kwargs) self.client = client self.organization = client.organization self.fields["config"].choices = Configuration.get_choices( self.organization) self.fields["address"].choices = [ ("none", "Myself") ] + Address.get_choices(self.organization) self.fields["media"].choices = Media.get_choices( kind=None if client.can_order_physical else Media.VIRTUAL, display_units=client.is_limited, ) self.fields["kind"].choices = filter( lambda item: client.can_order_physical or item[0] != Media. PHYSICAL, self.KIND_CHOICES.items(), )
def address_delete(request, address_id=None): address = Address.get_or_none(address_id) if address is None: raise Http404("Address not found") if address.organization != request.user.profile.organization: raise HttpResponse("Unauthorized", status=401) try: address.delete() messages.success( request, "Successfuly deleted Address <em>{}</em>".format(address)) except Exception as exp: logger.error("Unable to delete Address {id}: {exp}".format( id=address.id, exp=exp)) messages.error( request, "Unable to delete Address <em>{addr}</em>: -- ref {exp}".format( addr=address, exp=exp), ) return redirect("address_list")
def country_name(country_code): return Address.country_name_for(country_code)
def clean_address(self): address = Address.get_or_none(self.cleaned_data.get("address")) if address is None or address.organization != self.organization: raise forms.ValidationError("Not your address", code="invalid") return address