Example #1
0
 def pickle(self, files, total):
     
     newFiles = []
     
     for file in files:
         #logging.info('file dired: %s', dir(file))
         normalizedEntities = toNormalizedDict(file)
         newFiles.append(normalizedEntities)
         
     
     #TODO TAGS!!!!!
     
     #logging.info(newFiles)
     #JsonUtils.put(json, "totalResults", totalResults);
     #JsonUtils.put(json, "totalResultsFormatted", formatted);
     
     #totalResults = len(newFiles)
     #formatted = len(newFiles)
     
     # TODO: totalResultsFormatted needs to be:
     # 1) Really the total results
     # 2) Formatted.
     filesInstance = {
         'totalResults': total, 
         'totalResultsFormatted': total, 
         'results' : newFiles}
     json = jsonpickle.dumps(filesInstance)
     return json
Example #2
0
def publishFileBase(request, useInstanceId):
    requestDict = request.REQUEST
    logging.info('Handling publishFile request: %s', requestDict.items())
    title = requestDict.get('title')
    #logging.info('Getting SHA-1')
    sha1 = requestDict.get('sha1')
    #etag = requestDict.get('etag')
    uri = requestDict.get('uri')
    
    size = requestDict.get('size')
    
    #logging.info('Got params')
    if title[0] == '.':
        return HttpResponseBadRequest('Cannot publish dot files')
    
    #logging.info('About to guess MIME')
    mimeType, encoding = mimetypes.guess_type(title)
    #logging.info('full mime: %s', mimeType)
    if mimeType is None:
        mimeType = 'application/octet-stream'
    mediaType = typeTranslator.getType(title);
    #logging.info('mediaType: %s', mediaType)
    
    if useInstanceId:
        #logging.info('getting instance ID')
        instanceId = int(requestDict.get('instanceId'))
        file = models.File.gql('where sha1 = :1 and instanceId = :2', sha1, instanceId).get()
    else:
        file = models.File.gql('where uri = :1 and size = :2', uri, size).get()
        
    logging.info('Existing file is: %s', file)
    
    requestTags = requestDict.get('tags')
    tagsArray = toTags(requestTags)
    
    alreadyPublished = False
    if file is None:
        form = PublishFileForm(data=requestDict)
    else:
        logging.info('File is not None!!')
        form = PublishFileForm(data=requestDict, instance=file)
        alreadyPublished = True
    if not form.is_valid():
        logging.info('Form not valid!!')
        return HttpResponse('ERROR: Publish file errors:\n%s' % repr(form.errors),
                        content_type='text/plain', status=400)
        
    file = form.save(commit=False)
    file.downloaded = util.dictStringToBool(requestDict, 'downloaded')
    file.mimeType = mimeType
    file.mediaType = mediaType
    file.tags = tagsArray
    
    metaFile = models.MetaFile.gql('where uri = :1', form.cleaned_data['uri']).get()
    if metaFile is None:
        #logging.info('Creating new meta file')
        metaForm = PublishMetaFileForm(data=requestDict)
        if not metaForm.is_valid():
            logging.info('ERROR: Publish meta file errors:\n%s' % repr(metaForm.errors))
            return HttpResponse('ERROR: Publish meta file errors:\n%s' % repr(metaForm.errors),
                        content_type='text/plain', status=400)
        metaFile = metaForm.save(commit=False)
        metaTags = title.split()
        metaTags.extend(tagsArray)
        metaFile.tags = metaTags
        metaFile.titles = [metaFile.title]
        #metaFile.onlineInstances = []
        metaFile.mimeType = mimeType
        metaFile.mediaType = mediaType
        
    else:
        #logging.info('Updating meta file')
        if title not in metaFile.titles:
            metaFile.titles.append(title)
        
        metaTags = []
        for tag in tagsArray:
            if tag not in metaFile.tags:
                logging.info('Appending tag: %s', tag)
                metaTags.append(tag)
        metaFile.tags.extend(metaTags) 

    #logging.info('Saving metaFile: %s', metaFile)
    if useInstanceId:
        instance = models.OnlineInstance.gql('where instanceId = :1', instanceId).get()
        if instance is None:
            logging.error('We know nothing about the instance')
            file.instanceOnline = False
        else:
            file.instanceOnline = True
            logging.debug('Appending LittleShoot instance')
            #if instance.key() not in metaFile.onlineInstances:
            #logging.info('Adding instance...')
            #metaFile.onlineInstances.append(instance.key())
            
            # This could be a publish request that's updating an existing file,
            # in which case it shouldn't increase the online count.
            if not alreadyPublished:
                metaFile.numOnlineInstances += 1
            
    file.put()
    metaFile.put()
    
    logging.info("Encoding file title: %s", file.title)
    encodedName = urllib.quote(file.title)
    downloadParams = {'uri': metaFile.uri,
        'name': file.title,
        'size': metaFile.size,
        
        # Careful here. In the future the URI passed to this method might 
        # not be a URN.
        'urn': metaFile.uri
        }
    encodedDownloadParams = urllib.urlencode(downloadParams)
    fullUri = 'http://www.littleshoot.org/api/client/download/'+encodedName+'?' + encodedDownloadParams
    logging.info("Encoded URL: %s", fullUri)
    
    # The URI here needs to be the full
    params = {'uri': fullUri,
        'title': file.title,
        }
    #query = urllib.urlencode(params)
    #linkUrl = "http://www.littleshoot.org/link?"+ query
    
    allProps = toNormalizedDict(metaFile)
    #allProps['link'] = linkUrl
    allProps['link'] = fullUri
    
    logging.info("Props: %s", allProps)
    
    json = simplejson.dumps(allProps)
    
    channels = simplejson.loads(memcache.get('channels') or '{}')
    
    files = fetchRecentFiles("0", "10")
    encoded_message = filesPickler.pickle(files, 10)
    
    for channel_id in channels.iterkeys():
        # encoded_message = simplejson.dumps(message)
        logging.info("Sending message on channel: %s", channel_id)
        channel.send_message(channel_id, encoded_message)
        
    return jsonControllerUtils.writeResponse(request, json)