Exemplo n.º 1
0
 def create_item(self, parent, pk, kwargs):
     
     obj_dt = self.items_by_pk[pk]
     
     kwargs['pipe']['locale'] = 'fr'
     
     # create the item
     item = Item(slug=obj_dt['translations'][0]['fields']['slug'],
                 label=obj_dt['translations'][0]['fields']['label'],
                 title=obj_dt['translations'][0]['fields']['title'],
                 description=obj_dt['translations'][0]['fields']['description'],
                 content=obj_dt['translations'][0]['fields']['content'],
                 
                 published=obj_dt['data']['fields']['published'],
                 order=obj_dt['data']['fields']['order'],
                 
                 **kwargs['pipe'])
     if parent:
         item.parent_id = parent.id
     
     item.save()
     
     # add other translations
     for translation in obj_dt['translations'][1:]:
     
         translation = Translation(related_id=item.id,
                                     slug=translation['fields']['slug'],
                                     label=translation['fields']['label'],
                                     title=translation['fields']['title'],
                                     description=translation['fields']['description'],
                                     content=translation['fields']['content'],
                                    **kwargs['pipe'])
         translation.save()
     
     return item
Exemplo n.º 2
0
 def parse(self, **kwargs):
     data = self.raw_data
     keyword = self.input_data.get('keyword')
     
     for status in data['items']:
         
         id = status.get('id')
         link = status.get('htmlLink', id)
         name = status.get('name', id)
         title = status.get('summary', name)
         description = status.get('description', title)
         
         #extract_dates(description)
         
         item = Item()
         
         item.label = name
         
         item.related_link = link
         item.slug = id
         item.username = name
         item.label = keyword
         item.title = status.get('summary')
         item.description = description
         
         self.items.append(item)
     return self.items
Exemplo n.º 3
0
    def process_paste(self, request, user_profile, input_data, template_args, **kwargs):
        """
        Paste an item somewhere

        takes a uid as parameter and set's the item's parent to current node
        """
        
        # clear the markups
        paste_markups = Moderation.objects.filter(akey=user_profile.akey, 
                                                  visible=True,
                                                  action__in=('cut', 'copy'), 
                                                  status__in=('cutting', 'copying'), 
                                                  ).order_by('-ref_time')
        
        if paste_markups.count():
            # manage the first as it is the last geenrated markup
            markup = paste_markups[0]
            if markup.status == 'cutting':
                markup.related.parent = kwargs['node']
                markup.related.save()
                markup.status = 'cutted'
                markup.save()
            
            elif markup.status == 'copying':
                # create a clone
                item = Item(**markup.related.get_as_dict())
                item.parent = kwargs['node']
                item.status = 'copied'
                item.save()
                markup.status = 'copied'
                markup.save()
            
            # clear the markups
            for markup in paste_markups:
                markup.visible = False
                markup.save()
            
        return self.manage_item_pipe(request, user_profile, input_data, template_args, **kwargs)
Exemplo n.º 4
0
 def parse(self, **kwargs):
     """
     Parse obtained data dict
     """
     for status in self.raw_data:
         try:
             item = Item.objects.filter(slug=status.id, parent=self.node).order_by('-ref_time')[0]
         except:
             item = Item()
             item.pk = item.id = get_new_uuid()
             item.parent = self.node
             
             item.slug = str(status.id)
             item.label = 'Message'
             item.title = status.text
             
             item.username = '******'+status.user.name
             item.email = ''
             item.status = 'imported'
             item.locale = 'fr'
             item.akey = self.user_profile.akey
             item.path = self.node.related_url
             item.action = 'related'
             
             #item.save()
             print item.pk
             
         self.items.append(item)
     
     return self.items
Exemplo n.º 5
0
    def get_context_dict(self, request, user_profile, input_data, **kwargs):
        """
        Return the main template args with page hierarchy
        """
        action = kwargs.get('action')
        
        if request.path_info.endswith('.json'):
            request_path = request.path_info[:-len('.json')]
        else:
            request_path = request.path_info
        
        if request_path.endswith(action+'/'):
            request_path = request_path[:-len(action)-1]
        
        elif request_path.endswith(action):
            request_path = request_path[:-len(action)]
        
        # check for pagination pattern in end of path
        # my-slug-[1,2,3 ... 
        # my-slug-[A,B,C,D ...
        
        template_args = super(ContentView, self).get_context_dict(request, user_profile, input_data, **kwargs)
        
        item_path = Item.objects.get_clean_path(request_path)
        
        # remove action from path
        if item_path and item_path.endswith(action):
            item_path = item_path[:-len(action)-1]
        
        try:
            re_uuid = re.compile("[0-F]{8}-[0-F]{4}-[0-F]{4}-[0-F]{4}-[0-F]{12}", re.I)
            item_parts = Item.objects.get_clean_path(item_path).split('/')
            if len(item_parts) == 2 and re_uuid.findall(item_parts[1]):
                page = Item.objects.get(id=item_parts[1])
            else:
                page = Item.objects.get_at_url(request_path, exact=True)
            
        except ObjectDoesNotExist:
            
            # acces objects by uuid ?
            # remove first slash and check if request path is uid
            # http://stackoverflow.com/questions/136505/searching-for-uuids-in-text-with-regex
        
            try:
                page = Item.objects.get_at_url(item_path, exact=False)
                if not action in ('directory', ):
                    raise Http404
            
            except ObjectDoesNotExist:
                # this means the root item haven't been created yet
                # ensure we have a root page corresponding
                # so we create it as long as the system could not work otherwise
                if len(item_path.split('/')) == 1:
                    root_slug = item_path.split('/')[0]
                    page = Item(slug=root_slug,
                                label=root_slug,
                                title=root_slug,
                                username=user_profile.username,
                                email=user_profile.email,
                                akey=kwargs['pipe']['akey'],
                                action=kwargs['pipe']['action'],
                                path=kwargs['pipe']['path'],
                                locale=kwargs['pipe']['locale'],
                                status='added')
                    page.related_id = page.id
                    page.save()
                else:
                    print root_slug
                    raise Http404
            
        # Check for permission
        if page:
            
            rootNode = page.get_root()
            roots = rootNode.get_children().filter(visible=True).order_by('order')
            
            if page.parent:
                parentNodes = page.parent.get_children().filter(visible=True)
            else:
                parentNodes = rootNode.get_children().filter(visible=True)
            
            nodes = page.get_children()
            nodes = nodes.order_by('order')
            
            #i = 0
            #for n in nodes:
            #    if n.order != i:
            #        n.order = i
            #        n.save()
            #    i+=1
            
            #nodes = nodes[:100]
            
        else:
            raise Http404

        # Check for language
        template_args.update({'currentNode':page,
                              'parentNodes':parentNodes,
                              'rootNode':rootNode,
                              'roots':roots,
                              'nodes':nodes,})
        return template_args
Exemplo n.º 6
0
    def parse(self, **kwargs):
        """
        Parse obtained data dict
        """
        items = []
        for status in self.raw_data:
            try:
                item = Item.objects.filter(slug=status.id, parent=self.node).order_by("-ref_time")[0]
            except:
                item = Item()
                itrans = item.get_translation()
                item.pk = item.id = get_new_uuid()
                item.parent = self.node

                itrans.slug = str(status.id)

                item.username = "******" + status.user.name
                item.email = ""
                item.status = "imported"
                item.locale = "fr"
                # item.akey = ge

                item.path = self.node.related_url
                item.action = "related"

                item.label = self.input_data["keyword"]
                itrans.title = status.text
                # item.related_id = item.id
                if self.input_data.get("save"):
                    item.save()

                print item.pk
            items.append(item)
        self.items = item
        return items
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
0
    def process_push(self, request, user_profile, input_data, template_args, **kwargs):
        """
        Set the notebook to articles
        """
        node = kwargs['node']
        nb = get_or_create_notebook(node)
        
        i = 0
        # if not, create one
        for cell in nb.cells:
            # check for item corresponding
            try:
                cell_id = cell.metadata.gid
            except:
                cell.metadata.gid = get_new_uuid()
                cell_id = cell.metadata.gid
            
            try:
                cell_node = Item.objects.get(id=cell_id)
            except ObjectDoesNotExist:
                cell_node = Item(id=cell_id, parent=node, **kwargs['pipe'])
            
            if cell.cell_type == 'code':
                #
                content = u''
                for output in cell.outputs:
                    if output.output_type == 'stream' and 'stream' == 'stdout':
                        content += output.get('text', u'')
                cell_node.content = content
                
                #if cell_node.content:
                #    cell_node.visible = True
                #else:
                cell_node.visible = False
                
                #if not cell_node.title:
                #    cell_node.title = 'Code'
                
                #if not cell_node.label:
                #    cell_node.label = cell_node.title
                
            elif cell.cell_type == 'markdown':
                
                #cell_node.behavior = 'view'
                #cell_node.content = '<i>markdown rendered html</i>'
                
                cell_node.visible = True
                
                cell = markdown_to_item(cell_node, ''.join(cell.source))

            cell_node.status = 'pushed'
            cell_node.order = i
            cell_node.save()
            i += 1

        save_notebook(nb, node)
        
        #
        if not node.file:
            node.file = 'resource/'+node.id+'.ipynb'
            node.save()
        
        return HttpResponseRedirect(node.get_url()+'view/')

        return self.manage_item_pipe(request, user_profile, input_data, template_args, **kwargs)
Exemplo n.º 10
0
    def parse(self, **kwargs):
        
        token = 'CAACEdEose0cBAPemNCRC53fdEpBFWzVyCDBHWunsePzgZAW6U7xZAflPwmWW1GMixFpDSyhOLKdUR9GAV2BA3qqWdFBL2UoQDDRsvqFQ9n6sC1izct94BkKfakJZBrODfFB0NdZAxNrUyolxeojnWN829rvcX4gyLpkZBwcQJdnxeBBPpcZC4ojO1l00ZAbFESDx0vlcZCGSuzAHxPe13QfL'
        keyword = self.input_data.get('keyword')
        
        for status in self.raw_data:
            
            type = status.get('type')
            
            if type == 'event':
                event_id = status.get('object_id')
            elif type == 'status':
                event_id = status.get('id').split('_')[1]
            else:
                event_id = None
            
            # skip non event stuff
            if event_id:
                
                # get the event data
                graph_url = 'https://graph.facebook.com/'+event_id+'?access_token='+self.token
                r = requests.get(graph_url)
                event_data = r.json()
                
                # text
                label = status.get('name', id)
                title = event_data.get('name', id)
                
                description = event_data.get('description', title)
                content = ''

                link = status.get('link', id)
                
                # time
                timezone = event_data.get('timezone')
                start = event_data.get('start_time')
                end = event_data.get('end_time', start)
                
                # location
                location = event_data.get('location')
                event_venue = event_data.get('venue',{})
                
                address = ', '.join((event_venue.get('street', ''),
                                     event_venue.get('zip', ''),
                                     event_venue.get('city', ''),
                                     event_venue.get('country', ''),))
                
                latitude = event_venue.get('latitude', None)
                longitude = event_venue.get('longitude', None)
                
                #
                picture = status.get('link', id)
            else:
                print status
                    
                # text
                link = status.get('link', None)
                label = status.get('name', id)
                title = status.get('name', id)
                description = status.get('message', title)
                
                start = end = now()
                
                location = ''
                address = ''
                latitude = None
                longitude = None
             
            #
            if description:
                item = Item()
                item.slug = id
                item.related_url = link
                
                item.start = start
                item.end = end
                
                item.location = location
                item.address = address
                
                item.latitude = latitude
                item.longitude = longitude
                
                item.slug = id
                item.label = keyword
                item.title = title
                item.description = description
                item.content = content
                
                item.parent = self.node
                
                self.items.append(item)
        
            return self.items