Ejemplo n.º 1
0
class ProposalIndex(indexes.ModelSearchIndex, indexes.Indexable):
    text = CharField(document=True, use_template=True)
    active = BooleanField(model_attr='active')
    title = CharField(model_attr='title')
    community = IntegerField(model_attr='issue__community_id')
    status = IntegerField(model_attr='status')
    task_completed = BooleanField(model_attr='task_completed')
    type = IntegerField(model_attr='type')
    decided_at = DateTimeField()
    assignee = CharField()
    due_by = DateField(model_attr='due_by', null=True)
    is_confidential = BooleanField(model_attr='is_confidential')

    def get_model(self):
        return Proposal

    def prepare_assignee(self, obj):
        return u'' if not obj.assigned_to_user else \
                  obj.assigned_to_user.display_name

    def prepare_decided_at(self, obj):
        return obj.created_at if not obj.decided_at_meeting \
              else obj.decided_at_meeting.held_at

    # Note that regular ``SearchIndex`` methods apply.
    def index_queryset(self, using=None):
        "Used when the entire index for model is updated."
        return Proposal.objects.active()
Ejemplo n.º 2
0
class IssueIndex(indexes.ModelSearchIndex, indexes.Indexable):
    community = IntegerField(model_attr='community_id')
    is_confidential = BooleanField(model_attr='is_confidential')

    class Meta:
        model = Issue
        fields = ['title', 'abstract']

    # Note that regular ``SearchIndex`` methods apply.
    def index_queryset(self, using=None):
        "Used when the entire index for model is updated."
        return Issue.objects.active()
Ejemplo n.º 3
0
class ImageIndex(CelerySearchIndex, Indexable):
    text = CharField(document=True, use_template=True)

    title = CharField(model_attr='title')
    description = CharField(model_attr='description', null=True)
    published = DateTimeField(model_attr='published')
    uploaded = DateTimeField(model_attr='uploaded')
    imaging_telescopes = CharField()
    guiding_telescopes = CharField()
    mounts = CharField()
    imaging_cameras = CharField()
    guiding_cameras = CharField()
    pixel_scale = FloatField()
    field_radius = FloatField()
    countries = CharField()

    animated = BooleanField(model_attr='animated')

    likes = IntegerField()
    integration = FloatField()
    moon_phase = FloatField()
    first_acquisition_date = DateTimeField()
    last_acquisition_date = DateTimeField()
    views = IntegerField()

    solar_system_main_subject = IntegerField()

    is_deep_sky = BooleanField()
    is_solar_system = BooleanField()
    is_sun = BooleanField()
    is_moon = BooleanField()
    is_planets = BooleanField()
    is_comets = BooleanField()

    is_iotd = BooleanField()
    is_top_pick = BooleanField()

    license = IntegerField(model_attr='license')

    min_aperture = IntegerField()
    max_aperture = IntegerField()

    min_pixel_size = IntegerField()
    max_pixel_size = IntegerField()

    bookmarks = IntegerField()

    telescope_types = MultiValueField()
    camera_types = MultiValueField()

    comments = IntegerField()

    is_commercial = BooleanField()

    subject_type = IntegerField(model_attr='subject_type')

    acquisition_type = CharField(model_attr='acquisition_type')

    data_source = CharField(model_attr='data_source')

    remote_source = CharField(model_attr='remote_source', null=True)

    username = CharField(model_attr='user__username')

    objects_in_field = CharField()

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(moderator_decision=1).exclude(
            corrupted=True)

    def get_model(self):
        return Image

    def get_updated_field(self):
        return "updated"

    def prepare_imaging_telescopes(self, obj):
        return [
            "%s, %s" % (x.get("make"), x.get("name"))
            for x in obj.imaging_telescopes.all().values('make', 'name')
        ]

    def prepare_guiding_telescopes(self, obj):
        return [
            "%s, %s" % (x.get("make"), x.get("name"))
            for x in obj.guiding_telescopes.all().values('make', 'name')
        ]

    def prepare_mounts(self, obj):
        return [
            "%s, %s" % (x.get("make"), x.get("name"))
            for x in obj.mounts.all().values('make', 'name')
        ]

    def prepare_imaging_cameras(self, obj):
        return [
            "%s, %s" % (x.get("make"), x.get("name"))
            for x in obj.imaging_cameras.all().values('make', 'name')
        ]

    def prepare_guiding_cameras(self, obj):
        return [
            "%s, %s" % (x.get("make"), x.get("name"))
            for x in obj.guiding_cameras.all().values('make', 'name')
        ]

    def prepare_pixel_scale(self, obj):
        return obj.solution.pixscale if obj.solution else None

    def prepare_field_radius(self, obj):
        return obj.solution.radius if obj.solution else None

    def prepare_likes(self, obj):
        return _prepare_likes(obj)

    def prepare_integration(self, obj):
        return _get_integration(obj)

    def prepare_moon_phase(self, obj):
        return _prepare_moon_phase(obj)

    def prepare_first_acquisition_date(self, obj):
        return _prepare_first_acquisition_date(obj)

    def prepare_last_acquisition_date(self, obj):
        return _prepare_last_acquisition_date(obj)

    def prepare_views(self, obj):
        return _prepare_views(obj, 'image')

    def prepare_bookmarks(self, obj):
        return _prepare_bookmarks(obj)

    def prepare_telescope_types(self, obj):
        return _prepare_telescope_types(obj)

    def prepare_camera_types(self, obj):
        return _prepare_camera_types(obj)

    def prepare_comments(self, obj):
        return _prepare_comments(obj)

    def prepare_is_commercial(self, obj):
        commercial_gear = CommercialGear.objects.filter(image=obj)
        return commercial_gear.count() > 0

    def prepare_is_iotd(self, obj):
        return hasattr(obj, 'iotd')

    def prepare_is_top_pick(self, obj):
        return obj.iotdvote_set.count() > 0 and not hasattr(obj, 'iotd')

    def prepare_objects_in_field(self, obj):
        return obj.solution.objects_in_field.join(", ") \
            if obj.solution and obj.solution.objects_in_field \
            else None

    def prepare_countries(self, obj):
        return ' '.join([x.country for x in obj.locations.all() if x.country])
Ejemplo n.º 4
0
class TrapLocationIndex(indexes.SearchIndex, indexes.Indexable):
    text = CharField(document=True, use_template=True)

    species_id = CharField(faceted=True)

    habitat = CharField(faceted=True)
    school = CharField(faceted=True)
    observer_name = CharField(faceted=True)

    species_label = CharField(faceted=True)
    habitat_label = CharField(faceted=True)
    school_label = CharField(faceted=True)

    lat = CharField(faceted=True)
    lon = CharField(faceted=True)
    date = DateTimeField(faceted=True)
    trapped_and_released = BooleanField(faceted=True)
    observed = BooleanField(faceted=True)
    camera = BooleanField(faceted=True)
    tracks_and_signs = BooleanField(faceted=True)
    unsuccessful = BooleanField(faceted=True)
    real = BooleanField(faceted=True)

    asset_type = CharField(faceted=True)

    def get_model(self):
        return TrapLocation

    def prepare_species_id(self, obj):
        if obj.animal:
            return obj.animal.species.id
        else:
            return None

    def prepare_habitat(self, obj):
        if obj.habitat:
            return obj.habitat.id
        else:
            return None

    def prepare_school(self, obj):
        if obj.expedition.school:
            return obj.expedition.school.id
        else:
            return None

    def prepare_habitat_label(self, obj):
        if obj.habitat:
            return obj.habitat.label
        else:
            return None

    def prepare_species_label(self, obj):
        if obj.animal:
            return obj.animal.species.common_name
        else:
            return None

    def prepare_school_label(self, obj):
        if obj.expedition.school:
            return obj.expedition.school.name
        else:
            return None

    def prepare_date(self, obj):
        return obj.date_for_solr()

    def prepare_trapped_and_released(self, obj):
        if obj.animal:
            return True
        else:
            return False

    def prepare_unsuccessful(self, obj):
        if obj.animal:
            return False
        else:
            return True

    def prepare_real(self, obj):
        return obj.expedition.real

    def prepare_observer_name(self, obj):
        return obj.student_names

    # these are not sightings, so none of these three is applicable:
    def prepare_observed(self, obj):
        return None

    def prepare_camera(self, obj):
        return None

    def prepare_tracks_and_signs(self, obj):
        return None

    def prepare_lat(self, obj):
        return obj.actual_lat()

    def prepare_lon(self, obj):
        return obj.actual_lon()

    def prepare_asset_type(self, obj):
        return obj._meta.object_name
Ejemplo n.º 5
0
class SightingIndex(indexes.SearchIndex, indexes.Indexable):
    text = CharField(document=True, use_template=True)

    species_id = CharField(faceted=True)
    habitat = CharField(faceted=True)
    school = CharField(faceted=True)

    species_label = CharField(faceted=True)
    habitat_label = CharField(faceted=True)
    school_label = CharField(faceted=True)
    observer_name = CharField(faceted=True)

    date = DateTimeField(faceted=True)
    lat = CharField(faceted=True)
    lon = CharField(faceted=True)
    trapped_and_released = BooleanField(faceted=True)
    unsuccessful = BooleanField(faceted=True)
    observed = BooleanField(faceted=True)
    camera = BooleanField(faceted=True)
    tracks_and_signs = BooleanField(faceted=True)
    real = BooleanField(faceted=True)

    asset_type = CharField(faceted=True)

    def get_model(self):
        return Sighting

    def prepare_name(self, obj):
        return obj.__unicode__()

    def prepare_species_id(self, obj):
        if obj.species:
            return obj.species.id
        else:
            return None

    def prepare_habitat(self, obj):
        if obj.habitat:
            return obj.habitat.id
        else:
            return None

    def prepare_school(self, obj):
        return None

    def prepare_habitat_label(self, obj):
        if obj.habitat:
            return obj.habitat.label
        else:
            return None

    def prepare_species_label(self, obj):
        if obj.species:
            return obj.species.common_name
        else:
            return None

    def prepare_school_label(self, obj):
        return None

    def prepare_date(self, obj):
        return obj.date_for_solr()

    # these are not traps, so neither of these is applicable:
    def prepare_trapped_and_released(self, obj):
        return None

    def prepare_unsuccessful(self, obj):
        return None

    def prepare_real(self, obj):
        return True

    def prepare_observed(self, obj):
        if obj.observation_type and obj.observation_type.label in ['Sighting']:
            return True
        else:
            return False

    def prepare_camera(self, obj):
        if (obj.observation_type
                and obj.observation_type.label in ['Camera-trapped']):
            return True
        else:
            return False

    def prepare_tracks_and_signs(self, obj):
        if (obj.observation_type and obj.observation_type.label
                not in ['Sighting', 'Camera-trapped']):
            return True
        else:
            return False

    def prepare_asset_type(self, obj):
        return obj._meta.object_name

    def prepare_lat(self, obj):
        return obj.lat()

    def prepare_lon(self, obj):
        return obj.lon()

    def prepare_observer_name(self, obj):
        return "Black Rock Forest staff"
Ejemplo n.º 6
0
class ImageIndex(SearchIndex, Indexable):
    text = CharField(document=True, use_template=True)

    title = CharField(model_attr='title')
    description = CharField(model_attr='description', null=True)
    published = DateTimeField(model_attr='published')
    uploaded = DateTimeField(model_attr='uploaded')
    imaging_telescopes = CharField()
    guiding_telescopes = CharField()
    mounts = CharField()
    imaging_cameras = CharField()
    guiding_cameras = CharField()
    coord_ra_min = FloatField()
    coord_ra_max = FloatField()
    coord_dec_min = FloatField()
    coord_dec_max = FloatField()
    pixel_scale = FloatField()
    field_radius = FloatField()
    countries = CharField()

    animated = BooleanField(model_attr='animated')

    likes = IntegerField()
    integration = FloatField()
    moon_phase = FloatField()
    first_acquisition_date = DateTimeField()
    last_acquisition_date = DateTimeField()
    views = IntegerField()

    solar_system_main_subject_char = CharField(
        model_attr='solar_system_main_subject', null=True)

    is_deep_sky = BooleanField()
    is_solar_system = BooleanField()
    is_sun = BooleanField()
    is_moon = BooleanField()
    is_planets = BooleanField()
    is_comets = BooleanField()
    constellation = CharField()

    is_iotd = BooleanField()
    is_top_pick = BooleanField()
    is_top_pick_nomination = BooleanField()

    license = IntegerField()
    license_name = CharField()

    min_aperture = IntegerField()
    max_aperture = IntegerField()

    min_pixel_size = IntegerField()
    max_pixel_size = IntegerField()

    bookmarks = IntegerField()

    telescope_types = MultiValueField()
    camera_types = MultiValueField()

    comments = IntegerField()

    subject_type_char = CharField(model_attr='subject_type')

    acquisition_type = CharField(model_attr='acquisition_type')

    data_source = CharField(model_attr='data_source')

    remote_source = CharField(model_attr='remote_source', null=True)

    username = CharField(model_attr='user__username')

    objects_in_field = CharField()

    bortle_scale = FloatField()

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(moderator_decision=1).exclude(
            corrupted=True)

    def get_model(self):
        return Image

    def get_updated_field(self):
        return "updated"

    def prepare_imaging_telescopes(self, obj):
        return [
            "%s, %s" % (x.get("make"), x.get("name"))
            for x in obj.imaging_telescopes.all().values('make', 'name')
        ]

    def prepare_guiding_telescopes(self, obj):
        return [
            "%s, %s" % (x.get("make"), x.get("name"))
            for x in obj.guiding_telescopes.all().values('make', 'name')
        ]

    def prepare_mounts(self, obj):
        return [
            "%s, %s" % (x.get("make"), x.get("name"))
            for x in obj.mounts.all().values('make', 'name')
        ]

    def prepare_imaging_cameras(self, obj):
        return [
            "%s, %s" % (x.get("make"), x.get("name"))
            for x in obj.imaging_cameras.all().values('make', 'name')
        ]

    def prepare_guiding_cameras(self, obj):
        return [
            "%s, %s" % (x.get("make"), x.get("name"))
            for x in obj.guiding_cameras.all().values('make', 'name')
        ]

    def prepare_coord_ra_min(self, obj):
        if obj.solution is not None and obj.solution.ra is not None and obj.solution.radius is not None:
            return obj.solution.ra - obj.solution.radius
        return None

    def prepare_coord_ra_max(self, obj):
        if obj.solution is not None and obj.solution.ra is not None and obj.solution.radius is not None:
            return obj.solution.ra + obj.solution.radius
        return None

    def prepare_coord_dec_min(self, obj):
        if obj.solution is not None and obj.solution.dec is not None and obj.solution.radius is not None:
            return obj.solution.dec - obj.solution.radius
        return None

    def prepare_coord_dec_max(self, obj):
        if obj.solution is not None and obj.solution.dec is not None and obj.solution.radius is not None:
            return obj.solution.dec + obj.solution.radius
        return None

    def prepare_pixel_scale(self, obj):
        return obj.solution.pixscale if obj.solution else None

    def prepare_field_radius(self, obj):
        return obj.solution.radius if obj.solution else None

    def prepare_likes(self, obj):
        return _prepare_likes(obj)

    def prepare_integration(self, obj):
        return _prepare_integration(obj)

    def prepare_moon_phase(self, obj):
        return _prepare_moon_phase(obj)

    def prepare_first_acquisition_date(self, obj):
        return _prepare_first_acquisition_date(obj)

    def prepare_last_acquisition_date(self, obj):
        return _prepare_last_acquisition_date(obj)

    def prepare_views(self, obj):
        return _prepare_views(obj, 'image')

    def prepare_license(self, obj):
        return License.to_deprecated_integer(obj.license)

    def prepare_license_name(self, obj):
        return obj.license

    def prepare_bookmarks(self, obj):
        return _prepare_bookmarks(obj)

    def prepare_constellation(self, obj):
        constellation = ImageService.get_constellation(obj.solution)
        # Escape with __ because And (Andromeda) is not searchable, due to it being the same word as the AND operator.
        return "__%s__" % constellation.get(
            'abbreviation') if constellation else None

    def prepare_telescope_types(self, obj):
        return _prepare_telescope_types(obj)

    def prepare_camera_types(self, obj):
        return _prepare_camera_types(obj)

    def prepare_comments(self, obj):
        return _prepare_comments(obj)

    def prepare_is_iotd(self, obj):
        return IotdService().is_iotd(obj)

    def prepare_is_top_pick(self, obj):
        return IotdService().is_top_pick(
            obj) and not IotdService().is_iotd(obj)

    def prepare_is_top_pick_nomination(self, obj):
        return IotdService().is_top_pick_nomination(obj) and \
               not IotdService().is_top_pick(obj) and \
               not IotdService().is_iotd(obj)

    def prepare_objects_in_field(self, obj):
        return SolutionService(
            obj.solution).get_objects_in_field() if obj.solution else None

    def prepare_countries(self, obj):
        return ' '.join([x.country for x in obj.locations.all() if x.country])

    def prepare_bortle_scale(self, obj):
        deep_sky_acquisitions = DeepSky_Acquisition.objects.filter(
            image=obj, bortle__isnull=False)

        if deep_sky_acquisitions.exists():
            return sum([x.bortle for x in deep_sky_acquisitions]) / float(
                deep_sky_acquisitions.count())

        return None
Ejemplo n.º 7
0
class UserIndex(SearchIndex, Indexable):
    text = CharField(document=True, use_template=True)

    username = CharField(model_attr='username')
    exclude_from_competitions = BooleanField(
        model_attr='userprofile__exclude_from_competitions')

    avg_integration = FloatField()

    images = IntegerField()

    # Total likes of all user's images.
    likes = IntegerField()

    # Total likes given to images.
    likes_given = IntegerField()

    # Average likes of all user's images.
    average_likes = FloatField()

    # Normalized likes (Image Index)
    normalized_likes = FloatField()

    # Total likes received on comments.
    comment_likes_received = IntegerField()

    # Total likes received on forum posts.
    forum_post_likes_received = IntegerField()

    # Total number of likes on all "likeable" elements that can be associated to a user.
    total_likes_received = IntegerField()

    # User reputation based on text content
    # DEPRECATED: remove once contribution_index is populated
    reputation = FloatField()

    # Index based on text content
    contribution_index = FloatField()

    # Number of followers
    followers = IntegerField()

    # Total user integration.
    integration = FloatField()

    # Average moon phase under which this user has operated.
    moon_phase = FloatField()

    # Total views from all images.
    views = IntegerField()

    # Number of bookmarks on own images
    bookmarks = IntegerField()

    # Comments received on on own images
    comments = IntegerField()

    comments_written = IntegerField()

    forum_posts = IntegerField()

    top_pick_nominations = IntegerField()

    top_picks = IntegerField()

    iotds = IntegerField()

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

    def get_model(self):
        return User

    def get_updated_field(self):
        return "userprofile__updated"

    def prepare_images(self, obj):
        logger.info('Updating UserIndex: %d' % obj.pk)
        return Image.objects.filter(user=obj).count()

    def prepare_avg_integration(self, obj):
        integration = 0
        images = 0
        for i in Image.objects.filter(user=obj):
            cached = cache.get(PREPARED_INTEGRATION_CACHE_KEY % i.pk)
            image_integration = cached if cached is not None else _prepare_integration(
                i)
            if image_integration:
                images += 1
                integration += image_integration

        return (integration / 3600.0) / images if images else 0

    def prepare_likes(self, obj):
        likes = 0
        for i in Image.objects.filter(user=obj):
            cached = cache.get(PREPARED_LIKES_CACHE_KEY % i.pk)
            likes += cached if cached is not None else _prepare_likes(i)
        return likes

    def prepare_likes_given(self, obj):
        return ToggleProperty.objects.toggleproperties_for_model(
            'like', Image, obj).count()

    def prepare_average_likes(self, obj):
        likes = self.prepared_data.get('likes')
        if likes is None:
            likes = self.prepare_likes(obj)

        images = self.prepared_data.get('images')
        if images is None:
            images = self.prepare_images(obj)

        return likes / float(images) if images > 0 else 0

    def prepare_normalized_likes(self, obj):
        average = self.prepared_data.get('average_likes')

        if average is None:
            average = self.prepare_average_likes(obj)

        normalized = []

        for i in Image.objects.filter(user=obj).iterator():
            cached = cache.get(PREPARED_LIKES_CACHE_KEY % i.pk)
            likes = cached if cached is not None else i.likes()
            if likes >= average:
                normalized.append(likes)

        if len(normalized) == 0:
            result = 0
        else:
            result = _astrobin_index(normalized)

        if obj.userprofile.astrobin_index_bonus is not None:
            result += obj.userprofile.astrobin_index_bonus

        return result

    def prepare_comment_likes_received(self, obj):
        comments = NestedComment.objects.filter(author=obj)
        likes = 0
        for comment in comments.iterator():
            likes += ToggleProperty.objects.toggleproperties_for_object(
                'like', comment).count()

        return likes

    def prepare_forum_post_likes_received(self, obj):
        posts = Post.objects.filter(user=obj)
        likes = 0
        for post in posts.iterator():
            likes += ToggleProperty.objects.toggleproperties_for_object(
                'like', post).count()

        return likes

    def prepare_total_likes_received(self, obj):
        likes = self.prepared_data.get('likes')
        if likes is None:
            likes = self.prepare_likes(obj)

        comment_likes_received = self.prepared_data.get(
            'comment_likes_received')
        if comment_likes_received is None:
            comment_likes_received = self.prepare_comment_likes_received(obj)

        forum_post_likes_received = self.prepared_data.get(
            'forum_post_likes_received')
        if forum_post_likes_received is None:
            forum_post_likes_received = self.prepare_forum_post_likes_received(
                obj)

        return likes + comment_likes_received + forum_post_likes_received

    # DEPRECATED: remove once contribution_index is populated
    def prepare_reputation(self, obj):
        comments_contribution_index = _prepare_comment_contribution_index(
            NestedComment.objects.filter(author=obj))
        forum_post_contribution_index = _prepare_forum_post_contribution_index(
            Post.objects.filter(user=obj))
        return comments_contribution_index + forum_post_contribution_index

    def prepare_contribution_index(self, obj):
        comments_contribution_index = _prepare_comment_contribution_index(
            NestedComment.objects.filter(author=obj))
        forum_post_contribution_index = _prepare_forum_post_contribution_index(
            Post.objects.filter(user=obj))
        return comments_contribution_index + forum_post_contribution_index

    def prepare_followers(self, obj):
        return ToggleProperty.objects.filter(
            property_type="follow",
            content_type=ContentType.objects.get_for_model(User),
            object_id=obj.pk).count()

    def prepare_integration(self, obj):
        integration = 0
        for i in Image.objects.filter(user=obj):
            cached = cache.get(PREPARED_INTEGRATION_CACHE_KEY % i.pk)
            integration += cached if cached is not None else _prepare_integration(
                i)

        return integration / 3600.0

    def prepare_moon_phase(self, obj):
        l = []
        for i in Image.objects.filter(user=obj):
            cached = cache.get(PREPARED_MOON_PHASE_CACHE_KEY % i.pk)
            l.append(cached if cached is not None else _prepare_moon_phase(i))
        if len(l) == 0:
            return 0
        return reduce(lambda x, y: x + y, l) / len(l)

    def prepare_views(self, obj):
        views = 0
        for i in Image.objects.filter(user=obj):
            cached = cache.get(PREPARED_VIEWS_CACHE_KEY % i.pk)
            views += cached if cached is not None else _prepare_views(
                i, 'image')
        return views

    def prepare_bookmarks(self, obj):
        bookmarks = 0
        for i in Image.objects.filter(user=obj):
            cached = cache.get(PREPARED_BOOKMARKS_CACHE_KEY % i.pk)
            bookmarks += cached if cached is not None else _prepare_bookmarks(
                i)
        return bookmarks

    def prepare_comments(self, obj):
        comments = 0
        for i in Image.objects.filter(user=obj):
            cached = cache.get(PREPARED_COMMENTS_CACHE_KEY % i.pk)
            comments += cached if cached is not None else _prepare_comments(i)
        return comments

    def prepare_comments_written(self, obj):
        return NestedComment.objects.filter(author=obj, deleted=False).count()

    def prepare_forum_posts(self, obj):
        return Post.objects.filter(user=obj).count()

    def prepare_top_pick_nominations(self, obj):
        return IotdService().get_top_pick_nominations().filter(
            image__user=obj).count()

    def prepare_top_picks(self, obj):
        return IotdService().get_top_picks().filter(image__user=obj).count()

    def prepare_iotds(self, obj):
        return IotdService().get_iotds().filter(image__user=obj).count()