예제 #1
0
def contributorsSubscriber(obj, event):
    # Ensure that principals from the obj's version history are represented
    # in the Contributors field.
    
    def fixSeanTom(p):
        if p in ("T. Elliott", "Tom Elliott"):
            return "thomase"
        elif p in ("S. Gillies", "Sean Gillies", "admin"):
            return "sgillies"
        else:
            return p

    def repairPrincipal(p):
        return [fixSeanTom(v.strip()) for v in p.split(",")]
    
    def repairPrincipals(seq):
        return reduce(lambda x, y: x+y, map(repairPrincipal, seq), [])

    creators = set(repairPrincipals(obj.Creators()))
    contributors = set(filter(
        lambda x: x not in creators, 
        repairPrincipals(obj.Contributors())))
    credited = creators.union(contributors)
    
    def getPrincipals(ob):
        principals = set()
        context = aq_inner(ob)
        rt = getToolByName(context, "portal_repository")
        history = rt.getHistoryMetadata(context)
        if history:
            for i in range(len(history)):
                metadata = history.retrieve(i)['metadata']['sys_metadata']
                for p in repairPrincipal(metadata['principal']):
                    principals.add(p)
        return principals

    try:
        principals = getPrincipals(obj)
        if IPlace.providedBy(obj):
            for sub in (obj.getNames() + obj.getLocations()):
                sub_principals = set(
                    repairPrincipals(sub.Creators()) \
                    + repairPrincipals(sub.Contributors()))
                principals = principals.union(sub_principals)
        uncredited = principals - credited
        
        obj.setCreators(list(creators))
        obj.setContributors(list(contributors.union(uncredited)))
        obj.reindexObject(idxs=['Creator', 'Contributors'])
        
        context = aq_inner(obj)
        parent = aq_parent(context)
        if IPlace.providedBy(parent):
            contributorsSubscriber(parent, event)

    except:
        log.exception(
            "Failed to sync Contributors with revision history" )
예제 #2
0
 def __call__(self):
     ob = self.context
     while ob:
         if IPlace.providedBy(ob):
             break
         ob = aq_parent(aq_inner(ob))
     return ob
예제 #3
0
def locationActionSucceededSubscriber(obj, event):
    log.debug("Event handled: %s, %s", obj, event)
    reindexContainer(obj, event)
    context = aq_inner(obj)
    parent = aq_parent(context)
    if IPlace.providedBy(parent):
        contributorsSubscriber(parent, event)
예제 #4
0
def reindexContainer(obj, event):
    x = aq_inner(obj)
    f = aq_parent(x)
    if IPlace.providedBy(f):
        log.debug("Reindexing container %s", f)
        f.reindexObject()
        reindexWhole(f, event)
예제 #5
0
    def __call__(self, **kw):
        data = {}
        context = self.context

        if IPlace.providedBy(context):
            pid = context.getId()  # local id like "149492"
        elif ILocation.providedBy(context) or IName.providedBy(context):
            pid = aq_parent(aq_inner(context)).getId()
        else:
            pid = None

        if pid is not None:
            annotations = self._get_annotations(pid)
            if annotations is None:
                self.request.response.setStatus(500)
                annotations = []
            else:
                self.request.response.setStatus(200)
        else:
            annotations = []
            self.request.response.setStatus(404)

        data = dict(
            pid=pid,
            annotations=annotations)

        self.request.response.setHeader('Content-Type', 'application/json')
        return json.dumps(data)
예제 #6
0
 def placeId(self):
     ctx = self.context
     if IPlace.providedBy(ctx):
         return ctx.getId()
     elif ILocation.providedBy(ctx) or IName.providedBy(ctx):
         return aq_parent(aq_inner(ctx)).getId()
     else:
         return None
예제 #7
0
 def placeId(self):
     ctx = self.context
     if IPlace.providedBy(ctx):
         return ctx.getId()
     elif ILocation.providedBy(ctx) or IName.providedBy(ctx):
         return aq_parent(aq_inner(ctx)).getId()
     else:
         return None
예제 #8
0
    def __call__(self, **kw):
        data = {}
        context = self.context

        if IPlace.providedBy(context):
            pid = context.getId()  # local id like "149492"
        elif ILocation.providedBy(context) or IName.providedBy(context):
            pid = aq_parent(aq_inner(context)).getId()
        else:
            pid = "*"

        tag = "pleiades:*=" + pid
        try:
            related = self._get_related_photos(pid)
        except FlickrResponseError:
            related = {}

        if related:
            total = 0
            photos = related.get('photos')
            if photos:
                total = int(photos['total'])

            data['related'] = dict(total=total, url=FLICKR_TAGS_BASE + tag)

        try:
            pool = self._get_pool_photos(pid)
        except FlickrResponseError as e:
            pool = {}
            self.request.response.setStatus(e.message)

        if pool:
            total = 0
            photos = pool.get('photos')
            if photos:
                total = int(photos['total'])
            if total < 1:
                data['portrait'] = None
            else:
                # Sort found photos by number of views, descending
                most_viewed = sorted(photos['photo'],
                                     key=lambda p: p['views'],
                                     reverse=True)
                if pid == "*":
                    photo = choice(most_viewed)
                else:
                    photo = most_viewed[0]

                title = "%s by %s." % (photo['title'].rstrip(" ."),
                                       photo['ownername'])
                data['portrait'] = dict(title=title,
                                        img=IMG_TMPL % photo,
                                        page=PAGE_TMPL % photo)

        self.request.response.setHeader('Content-Type', 'application/json')
        return simplejson.dumps(data)
예제 #9
0
    def __call__(self, **kw):
        data = {}
        context = self.context

        if IPlace.providedBy(context):
            pid = context.getId()  # local id like "149492"
        elif ILocation.providedBy(context) or IName.providedBy(context):
            pid = aq_parent(aq_inner(context)).getId()
        else:
            pid = "*"

        tag = "pleiades:*=" + pid
        try:
            related = self._get_related_photos(pid)
        except FlickrResponseError:
            related = {}

        if related:
            total = 0
            photos = related.get('photos')
            if photos:
                total = int(photos['total'])

            data['related'] = dict(total=total, url=FLICKR_TAGS_BASE + tag)

        try:
            pool = self._get_pool_photos(pid)
        except FlickrResponseError as e:
            pool = {}
            self.request.response.setStatus(e.message)

        if pool:
            total = 0
            photos = pool.get('photos')
            if photos:
                total = int(photos['total'])
            if total < 1:
                data['portrait'] = None
            else:
                # Sort found photos by number of views, descending
                most_viewed = sorted(
                    photos['photo'], key=lambda p: p['views'], reverse=True )
                if pid == "*":
                    photo = choice(most_viewed)
                else:
                    photo = most_viewed[0]

                title = "%s by %s." % (
                    photo['title'].rstrip(" ."), photo['ownername'] )
                data['portrait'] = dict(
                    title=title, img=IMG_TMPL % photo, page=PAGE_TMPL % photo )

        self.request.response.setHeader('Content-Type', 'application/json')
        return simplejson.dumps(data)
예제 #10
0
    def __call__(self, **kw):
        data = {}
        context = self.context
        
        if IPlace.providedBy(context):
            pid = context.getId() # local id like "149492"
        elif ILocation.providedBy(context) or IName.providedBy(context):
            pid = aq_parent(aq_inner(context)).getId()
        else:
            pid = None

        if pid is not None:
            try:
                annotations = client.annotations(pid)
                self.request.response.setStatus(200)
            except client.PelagiosAPIError, e:
                annotations = []
                log.exception("Pelagios API Error: %s", str(e))
                self.request.response.setStatus(500)
예제 #11
0
def createGeoItem(context):
    """Factory for adapters."""
    if IPlace.providedBy(context):
        return PlaceGeoItem(context)
    else:
        return FeatureGeoItem(context)
예제 #12
0
def createGeoItem(context):
    """Factory for adapters."""
    if IPlace.providedBy(context):
        return PlaceGeoItem(context)
    else:
        return FeatureGeoItem(context)
예제 #13
0
def nameActionSucceededSubscriber(obj, event):
    reindexContainer(obj, event)
    context = aq_inner(obj)
    parent = aq_parent(context)
    if IPlace.providedBy(parent):
        contributorsSubscriber(parent, event)