Ejemplo n.º 1
0
 def get_forms_instances(self, action, user_profile, kwargs):
     
     if action in UIView.class_actions:
         
         if action in ('translate', 'redirect', 'code', 'rename'):
             
             # load a new translation cloning the existing one
             trans_data = model_to_dict(kwargs['node'])
             
             user_data = model_to_dict(user_profile)
             
             trans_data.update(user_data)
             for key in trans_data.keys():
                 if not key in Item.__localizable__:
                     del trans_data[key]
             
             trans_data['locale'] = get_language()
             trans_data['path'] = kwargs['node'].path
             
             trans_data['akey'] = user_profile.akey
             trans_data['username'] = user_profile.username
             trans_data['email'] = user_profile.email
             trans_data['validated'] = user_profile.validated
             
             trans_data['related_id'] = kwargs['node'].id
             
             trans_data['action'] = kwargs['action']
             
             trans_data['status'] = 'changed'
             trans_data['subject'] = 'Changed texts'
             trans_data['message'] = 'Text have been changed'
             trans_data['visible'] = True
             
             translation = Translation(**trans_data)
             
             return (translation,)
         
         elif action in ('add',):
             parentNode = kwargs['node']
             
             item = Item()
             item.parent = parentNode
             item.locale = get_language()
             item.get_translation()
             
             item.akey=user_profile.akey
             item.username=user_profile.username
             item.email=user_profile.email
             item.validated=user_profile.validated
             
             item.action='add'
             item.status='added'
             item.subject='Ajout'
             item.message='Nouvel element ajoute'
             
             item.path = parentNode.get_path()
             
             
             item.related_id = item.id
             
             return (item,)
         else:
             return (kwargs['node'],)
             #kwargs['node'].get_url()
             #return (Item.objects.get(id=kwargs['node'].id),)
     else:
         return super(UIView, self).get_forms_instances(action, user_profile, kwargs)
Ejemplo n.º 2
0
 def process_upload(self, request, user_profile, input_data, template_args, noajax=False, **kwargs):
     """
     Main Multiuploader module.
     Parses data from jQuery plugin and makes database changes.
     """
     
     if request.method == 'POST':
         log.info('received POST to main multiuploader view')
 
         if request.FILES is None:
             response_data = [{"error": _('Must have files attached!')}]
             return HttpResponse(json.dumps(response_data))
 
         if not u'form_type' in request.POST:
             response_data = [{"error": _("Error when detecting form type, form_type is missing")}]
             return HttpResponse(json.dumps(response_data))
 
         signer = Signer()
 
         try:
             form_type = signer.unsign(request.POST.get(u"form_type"))
         except BadSignature:
             response_data = [{"error": _("Tampering detected!")}]
             return HttpResponse(json.dumps(response_data))
 
         form = MultiUploadForm(request.POST, request.FILES, form_type=form_type)
         
         if not form.is_valid():
             error = _("Unknown error")
 
             if "file" in form._errors and len(form._errors["file"]) > 0:
                 error = form._errors["file"][0]
 
             response_data = [{"error": error}]
             return HttpResponse(json.dumps(response_data))
         
         rfile = request.FILES[u'file']
         wrapped_file = UploadedFile(rfile)
         filename = wrapped_file.name
         file_size = wrapped_file.file.size
         
         fname, ext = os.path.splitext(filename)
         
         as_image = False
         if ext.lower() in ('.jpg', '.jpeg', '.png', '.gif'):
             as_image = True
         
         
         log.info('Got file: "%s"' % filename)
         
         itemModel = template_args.get('currentNode')
         
         fl = Item(id=get_new_uuid())
         
         fl.action = 'upload'
         fl.path = itemModel.get_url()
         
         fl.akey = user_profile.akey
         
         fl.email = user_profile.email
         fl.username = user_profile.username
         
         fl.validated = user_profile.validated
         
         fl.slug = slugify(filename)
         fl.title = filename
         fl.parent = itemModel
         
         fl.file = rfile
         
         if as_image == True:
             fl.behavior = 'image'
             fl.label = input_data.get('label', 'Image')
             fl.order = input_data.get('order', 0)
             fl.image = rfile
         else:
             # TODO
             fl.behavior = 'upload'
             fl.label = input_data.get('label', 'Fichier')
             fl.order = input_data.get('order', 0)
             # set screenshot of document or icon
             #fl.image = rfile
         
         fl.status = 'uploaded'
         fl.locale = get_language()
         
         fl.subject = 'Fichier ajoute'
         fl.message = 'Le fichier '+filename+' a ete ajoute'
         
         fl.save()
         
         thumb_url = "" #get_thumbnail(fl.file, "80x80", quality=50)
         file_link_url = "" #reverse('multiuploader_file_link', args=[fl.pk]),
         delete_url = "" #reverse('multiuploader_delete', args=[fl.pk])
         
         #generating json response array
         result = [{"id": str(fl.id),
                    "name": filename,
                    "size": file_size,
                    "url": file_link_url,
                    "thumbnail_url": thumb_url,
                    "delete_url": delete_url,
                    "delete_type": "POST", }]
         
         response_data = json.dumps(result)
         
         #checking for json data type
         #big thanks to Guy Shapiro
         
         if noajax:
             if request.META['HTTP_REFERER']:
                 redirect(request.META['HTTP_REFERER'])
         
         if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
             mimetype = 'application/json'
         else:
             mimetype = 'text/plain'
         return HttpResponse(response_data, content_type=mimetype)
     else:  # GET
         template_args['multiuploader_form'] = MultiUploadForm(form_type='default')
     
     return self.manage_item_pipe(request, user_profile, input_data, template_args, **kwargs)
     return self.render(request, template_args, **kwargs)