def post(self, request, identifier): """Allow the creator to send updates, make changes etc.""" tree = self.get_object() kwargs = request.POST result = {} # Flag the Tree if 'tree' in kwargs: # just mail managers to deal with it tree.flagged = True tree.save() EmailTemplate.send( 'admin_flagged', request=request, obj=tree, ) result['tree'] = { 'message': 'OK', 'flag': tree.id, } # Flag a sighting if 'sighting' in kwargs: flag_sighting = kwargs['sighting'] try: sighting = tree.sighting_set.get(id=flag_sighting) except Sighting.DoesNotExist: raise Http404 if request.user == tree.creator: # remove this straight away. sighting.hidden = True sighting.flagged = True sighting.save() result['sighting'] = { 'message': 'OK', 'remove': flag_sighting, } else: # set a flag, and maybe alert the owner/ADAPT sighting.flagged = True sighting.save() EmailTemplate.send( 'admin_flagged', request=request, obj=sighting, ) result['sighting'] = { 'message': 'OK', 'flag': flag_sighting, } return HttpResponse(json.dumps(result), content_type="application/json; charset=utf-8")
def post(self, request, identifier): """Allow the creator to send updates, make changes etc.""" tree = self.get_object() kwargs = request.POST result = {} # Flag the Tree if 'tree' in kwargs: # just mail managers to deal with it tree.flagged = True tree.save() EmailTemplate.send('admin_flagged', request=request, obj=tree, ) result['tree'] = { 'message': 'OK', 'flag': tree.id, } # Flag a sighting if 'sighting' in kwargs: flag_sighting = kwargs['sighting'] try: sighting = tree.sighting_set.get(id=flag_sighting) except Sighting.DoesNotExist: raise Http404 if request.user == tree.creator: # remove this straight away. sighting.hidden = True sighting.flagged = True sighting.save() result['sighting'] = { 'message': 'OK', 'remove': flag_sighting, } else: # set a flag, and maybe alert the owner/ADAPT sighting.flagged = True sighting.save() EmailTemplate.send('admin_flagged', request=request, obj=sighting, ) result['sighting'] = { 'message': 'OK', 'flag': flag_sighting, } return HttpResponse( json.dumps(result), content_type="application/json; charset=utf-8")
class EventHandler(processing.EventHandler): def handle_shipping_event(self, order, event_type, lines, line_quantities, **kwargs): changes = False new_status = event_type.name try: if order.status != new_status: changes = True order.set_status(new_status) for line in lines: if line.status != new_status: changes = True line.set_status(new_status) except (InvalidOrderStatus, InvalidLineStatus), e: raise InvalidShippingEvent(str(e)) if not changes: raise InvalidShippingEvent( 'The select order lines are already marked as %s' % new_status) super(EventHandler, self).handle_shipping_event(order, event_type, lines, line_quantities, **kwargs) if event_type.name == 'Shipped': EmailTemplate.send('order_shipped', [order.email], order=order, lines=lines)
def post(self, request): self.request = request form_class = self._get_form_class(request) files = None if not request.FILES and request.POST.get('image_name', False): files = self.get_FILES(request.POST.get('image'), request.POST.get('image_name')) else: files = request.FILES form = form_class(request, request.user, request.POST, files) if form.is_valid(): sighting = form.save(commit=False) if request.user.is_authenticated(): sighting.creator_email = request.user.email sighting.tree = form.cleaned_data['tree'] duplicate = sighting.is_duplicate() if duplicate: # It is a duplicate, so lets just pretend we just saved # the original (and ignore the unsaved sighting we have here) if sighting.tree.sighting_set.count() == 0: sighting.tree.delete() return self.success_response(request, duplicate) sighting.save() self._save_survey(request, sighting) # Create the thumbnails as a background task create_thumbnails.delay(sighting.image) if sighting.tree.creator_email != sighting.creator_email: EmailTemplate.send( 'new_sighting_to_owner', to_emails=[sighting.tree.creator_email], sighting=sighting, request=request, ) return self.success_response(request, sighting) else: return render(request, self.template_name, self.get_context_data(form=form))
def form_valid(self, form): tree = get_object_or_404(Tree, id=self.kwargs['id']) Sighting.objects.create( tree=tree, creator_email=form.user.email, notes="I claimed this tree!", image=form.files['image'], location=tree.location, ) tree.tag_number = form.data['tag_number'] tree.save() EmailTemplate.send('admin_new_tag', tree=tree, request=self.request, this_tree_was='claimed from a previous sighting', ) return super(ClaimView, self).form_valid(form)
def clean(self): """Sort out the tag number""" c_data = self.cleaned_data c_data['tree'] = None tag_number = c_data.get('tag_number') if tag_number: try: c_data['tree'] = Tree.objects.get( tag_number=tag_number) except Tree.DoesNotExist: if self.user.is_authenticated(): # Then we can make a new tagged tree and attribute it to # this user. ADAPT will verify. tree = Tree.objects.create( location=c_data.get('location'), creator_email=self.user.email, tag_number=tag_number ) c_data['tree'] = tree EmailTemplate.send('admin_new_tag', tree=tree, request=self.request, this_tree_was='tagged from a new sighting', ) else: self._errors["tag_number"] = self.error_class([( "Hey! It looks like we haven't seen this tag number " "yet. If you are trying to claim it, please log in " "first!")]) else: email = self.user.email if self.user.is_authenticated() else c_data['creator_email'] c_data['tree'] = Tree.objects.create( location=c_data.get('location'), creator_email=email ) EmailTemplate.send('new_untagged_tree', to_emails=[email], request=self.request, ) return c_data
def form_valid(self, form): tree = get_object_or_404(Tree, id=self.kwargs['id']) Sighting.objects.create( tree=tree, creator_email=form.user.email, notes="I claimed this tree!", image=form.files['image'], location=tree.location, ) tree.tag_number = form.data['tag_number'] tree.save() EmailTemplate.send( 'admin_new_tag', tree=tree, request=self.request, this_tree_was='claimed from a previous sighting', ) return super(ClaimView, self).form_valid(form)
def post(self, request): self.request = request form_class = self._get_form_class(request) files = None if not request.FILES and request.POST.get('image_name', False): files = self.get_FILES(request.POST.get('image'), request.POST.get('image_name')) else: files = request.FILES form = form_class(request, request.user, request.POST, files) if form.is_valid(): sighting = form.save(commit=False) if request.user.is_authenticated(): sighting.creator_email = request.user.email sighting.tree = form.cleaned_data['tree'] duplicate = sighting.is_duplicate() if duplicate: # It is a duplicate, so lets just pretend we just saved # the original (and ignore the unsaved sighting we have here) if sighting.tree.sighting_set.count() == 0: sighting.tree.delete() return self.success_response(request, duplicate) sighting.save() self._save_survey(request, sighting) # Create the thumbnails as a background task create_thumbnails.delay(sighting.image) if sighting.tree.creator_email != sighting.creator_email: EmailTemplate.send('new_sighting_to_owner', to_emails=[sighting.tree.creator_email], sighting=sighting, request=request, ) return self.success_response(request, sighting) else: return render(request, self.template_name, self.get_context_data(form=form))