def create(self, request, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST if not self.check_create_permission(request, "x"): return rc.FORBIDDEN order = getOrNone( SaleOrder, pk=request.data['order']) if 'order' in request.data else None product = getOrNone( OrderedProduct, pk=request.data['product']) if 'product' in request.data else None productset = getOrNone( Product, pk=request.data['productset']) if 'productset' in request.data else None subscription = Subscription() if order: subscription.client = order.client if product: subscription.product = product.product if productset: subscription.product = productset form = SubscriptionForm( request.user.profile, request.data, instance=subscription) if form.is_valid(): subscription = form.save(commit=False) subscription.renew() subscription.save() subscription.set_user_from_request(request) if product: product.subscription = subscription product.save() return subscription else: self.status = 400 return form.errors
def flatten_dict(self, request): dct = super(SaleOrderHandler, self).flatten_dict(request) if request.method.lower() == 'post': if 'lead' in request.data: dct['lead'] = getOrNone(Lead, request.data['lead']) if 'opportunity' in request.data: dct['opportunity'] = getOrNone(Opportunity, request.data['opportunity']) else: dct['lead'] = None dct['opportunity'] = None return dct
def flatten_dict(self, request): dct = super(SaleOrderHandler, self).flatten_dict(request) if request.method.lower() == 'post': if 'lead' in request.data: dct['lead'] = getOrNone(Lead, request.data['lead']) if 'opportunity' in request.data: dct['opportunity'] = getOrNone( Opportunity, request.data['opportunity']) else: dct['lead'] = None dct['opportunity'] = None return dct
def create(self, request, object_ptr, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST order = getOrNone(SaleOrder, pk=object_ptr) if not order: return rc.NOT_FOUND if not request.user.profile.has_permission(order, mode='x'): return rc.FORBIDDEN ordered_product = OrderedProduct() ordered_product.order = order form = OrderedProductForm(request.user.profile, order, request.data, instance=ordered_product) if form.is_valid(): ordered_product = form.save(commit=False) convert(ordered_product, 'rate', currency=ordered_product.order.currency) ordered_product.set_user_from_request(request) ordered_product.order.update_total() return ordered_product else: self.status = 400 return form.errors
def flatten_dict(self, request): dct = super(OpportunityHandler, self).flatten_dict(request) if request.method.lower() == 'post' and 'lead' in request.data: dct['lead'] = getOrNone(Lead, request.data['lead']) else: dct['lead'] = None return dct
def update(self, request, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST pkfield = kwargs.get(self.model._meta.pk.name) or request.data.get( self.model._meta.pk.name) if not pkfield: return rc.BAD_REQUEST item = getOrNone(self.model, pkfield) if not item: return rc.NOT_FOUND if not request.user.profile.has_permission(item, mode="w"): return rc.FORBIDDEN attrs = self.flatten_dict(request) form = ItemForm(item_type=item.item_type, instance=item, **attrs) if form.is_valid(): item = form.save(request) return item else: self.status = 400 return form.errors
def create(self, request, object_ptr, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST order = getOrNone(SaleOrder, pk=object_ptr) if not order: return rc.NOT_FOUND if not request.user.profile.has_permission(order, mode='x'): return rc.FORBIDDEN ordered_product = OrderedProduct() ordered_product.order = order form = OrderedProductForm( request.user.profile, order, request.data, instance=ordered_product) if form.is_valid(): ordered_product = form.save(commit=False) convert( ordered_product, 'rate', currency=ordered_product.order.currency) ordered_product.set_user_from_request(request) ordered_product.order.update_total() return ordered_product else: self.status = 400 return form.errors
def flatten_dict(self, request): dct = super(KnowledgeFolderHandler, self).flatten_dict(request) dct['knowledgeType_id'] = None parent = request.data.get('parent') if parent: parent = getOrNone(KnowledgeFolder, pk=parent) if not parent or not request.user.profile.has_permission(parent, mode='x'): request.data['parent'] = None return dct
def create(self, request, *args, **kwargs): "Send email to some recipients" user = request.user.profile if request.data is None: return rc.BAD_REQUEST if 'stream' in request.data: stream = getOrNone(MessageStream, request.data['stream']) if stream and not user.has_permission(stream, mode='x'): return rc.FORBIDDEN message = Message() message.author = user.get_contact() if not message.author: return rc.FORBIDDEN form = MessageForm(user, None, None, request.data, instance=message) if form.is_valid(): message = form.save() message.recipients.add(user.get_contact()) message.set_user_from_request(request) message.read_by.add(user) try: # if email entered create contact and add to recipients if 'multicomplete_recipients' in request.POST and request.POST[ 'multicomplete_recipients']: try: conf = ModuleSetting.get_for_module( 'anaf.messaging', 'default_contact_type')[0] default_contact_type = ContactType.objects.get( pk=long(conf.value)) except Exception: default_contact_type = None emails = request.POST['multicomplete_recipients'].split( ',') for email in emails: emailstr = unicode(email).strip() if re.match( '[a-zA-Z0-9+_\-\.]+@[0-9a-zA-Z][.-0-9a-zA-Z]*.[a-zA-Z]+', emailstr): contact, created = Contact.get_or_create_by_email( emailstr, contact_type=default_contact_type) message.recipients.add(contact) if created: contact.set_user_from_request(request) except: pass # send email to all recipients message.send_email() return message else: self.status = 400 return form.errors
def create(self, request, *args, **kwargs): "Send email to some recipients" user = request.user.profile if request.data is None: return rc.BAD_REQUEST if 'stream' in request.data: stream = getOrNone(MessageStream, request.data['stream']) if stream and not user.has_permission(stream, mode='x'): return rc.FORBIDDEN message = Message() message.author = user.get_contact() if not message.author: return rc.FORBIDDEN form = MessageForm(user, None, None, request.data, instance=message) if form.is_valid(): message = form.save() message.recipients.add(user.get_contact()) message.set_user_from_request(request) message.read_by.add(user) try: # if email entered create contact and add to recipients if 'multicomplete_recipients' in request.POST and request.POST['multicomplete_recipients']: try: conf = ModuleSetting.get_for_module( 'anaf.messaging', 'default_contact_type')[0] default_contact_type = ContactType.objects.get( pk=long(conf.value)) except Exception: default_contact_type = None emails = request.POST[ 'multicomplete_recipients'].split(',') for email in emails: emailstr = unicode(email).strip() if re.match('[a-zA-Z0-9+_\-\.]+@[0-9a-zA-Z][.-0-9a-zA-Z]*.[a-zA-Z]+', emailstr): contact, created = Contact.get_or_create_by_email( emailstr, contact_type=default_contact_type) message.recipients.add(contact) if created: contact.set_user_from_request(request) except: pass # send email to all recipients message.send_email() return message else: self.status = 400 return form.errors
def create(self, request, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST if not self.check_create_permission(request, "x"): return rc.FORBIDDEN order = getOrNone( SaleOrder, pk=request.data['order']) if 'order' in request.data else None product = getOrNone( OrderedProduct, pk=request.data['product']) if 'product' in request.data else None productset = getOrNone(Product, pk=request.data['productset'] ) if 'productset' in request.data else None subscription = Subscription() if order: subscription.client = order.client if product: subscription.product = product.product if productset: subscription.product = productset form = SubscriptionForm(request.user.profile, request.data, instance=subscription) if form.is_valid(): subscription = form.save(commit=False) subscription.renew() subscription.save() subscription.set_user_from_request(request) if product: product.subscription = subscription product.save() return subscription else: self.status = 400 return form.errors
def create(self, request, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST item_type = getOrNone(ItemType, request.data.get('type')) if not item_type or not request.user.profile.has_permission(item_type, mode='x'): return rc.FORBIDDEN attrs = self.flatten_dict(request) form = ItemForm(item_type=item_type, **attrs) if form.is_valid(): item = form.save(request) return item else: self.status = 400 return form.errors
def create(self, request, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST contact_type = getOrNone(ContactType, request.data.get('contact_type')) if not contact_type or not request.user.profile.has_permission(contact_type, mode='x'): return rc.FORBIDDEN attrs = self.flatten_dict(request) form = ContactForm(contact_type=contact_type, **attrs) if form.is_valid(): contact = form.save(request, contact_type) contact.set_user_from_request(request) return contact else: self.status = 400 return form.errors
def create(self, request, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST contact_type = getOrNone(ContactType, request.data.get('contact_type')) if not contact_type or not request.user.profile.has_permission( contact_type, mode='x'): return rc.FORBIDDEN attrs = self.flatten_dict(request) form = ContactForm(contact_type=contact_type, **attrs) if form.is_valid(): contact = form.save(request, contact_type) contact.set_user_from_request(request) return contact else: self.status = 400 return form.errors
def check_create_permission(self, request, mode): if 'folder' in request.data: folder = getOrNone(Folder, pk=request.data['folder']) if not request.user.profile.has_permission(folder, mode='x'): request.data['folder'] = None return True
def check_create_permission(self, request, mode): if "folder" in request.data: folder = getOrNone(Folder, pk=request.data["folder"]) if not request.user.profile.has_permission(folder, mode="x"): request.data["folder"] = None return True