Beispiel #1
0
 def update_location(self, location_object):
     serialized_location = LocationSerializer(data=location_object)
     if serialized_location.is_valid():
         location = Location(**serialized_location.validated_data)
         location.user = self.__user
         location.time = timezone.now()
         location.category = self.categorize_location(
             latitude=location.latitude, longitude=location.longitude)
         location.save()
         return location
     else:
         raise self.InvalidLocation()
Beispiel #2
0
class PositionListingSerializer(serializers.ModelSerializer):
    position = PositionTypeSerializer()
    location = LocationSerializer()

    class Meta:
        model = PositionListing
        fields = ["id", "position", "location", "post_date"]
Beispiel #3
0
class EventFullSerializer(serializers.ModelSerializer):
    """Serializer for Event with more information.

    Returns a nested list of followers of each status and
    detailed information on venues.
    """

    from bodies.serializer_min import BodySerializerMin
    from locations.serializers import LocationSerializer
    from locations.models import Location
    from bodies.models import Body

    interested_count = serializers.SerializerMethodField()
    get_interested_count = lambda self, obj: FollowersMethods.get_count(obj, 1)

    going_count = serializers.SerializerMethodField()
    get_going_count = lambda self, obj: FollowersMethods.get_count(obj, 2)

    interested = serializers.SerializerMethodField()
    get_interested = lambda self, obj: FollowersMethods.get_followers(obj, 1)

    going = serializers.SerializerMethodField()
    get_going = lambda self, obj: FollowersMethods.get_followers(obj, 2)

    venues = LocationSerializer(many=True, read_only=True)
    venue_names = serializers.SlugRelatedField(many=True,
                                               read_only=True,
                                               slug_field='name',
                                               source='venues')
    venue_ids = serializers.PrimaryKeyRelatedField(
        many=True,
        read_only=False,
        source='venues',
        queryset=Location.objects.all(),
        required=False)

    bodies = BodySerializerMin(many=True, read_only=True)
    bodies_id = serializers.PrimaryKeyRelatedField(many=True,
                                                   read_only=False,
                                                   queryset=Body.objects.all(),
                                                   source='bodies')

    class Meta:
        model = Event
        fields = ('id', 'str_id', 'name', 'description', 'image_url',
                  'start_time', 'end_time', 'all_day', 'venues', 'venue_names',
                  'bodies', 'bodies_id', 'interested_count', 'going_count',
                  'interested', 'going', 'venue_ids', 'website_url')

    def to_representation(self, instance):
        result = super().to_representation(instance)
        # Remove unnecessary fields
        result.pop('venue_ids')
        return result

    def create(self, validated_data):
        result = super().create(validated_data)
        result.created_by = self.context['request'].user.profile
        result.save()
        return result
Beispiel #4
0
class VisitedLocationSerializer(serializers.Serializer):
    location = LocationSerializer()
    # CaseRecord = CaseRecordSerializer()
    # class Meta:
    #     model = VisitedLocation
    #     fields = ('dateFrom', 'dateTo', 'category','location','caseID')

    dateFrom = serializers.DateField()
    dateTo = serializers.DateField()
    category = serializers.CharField(max_length=20)
    caseID = serializers.IntegerField()

    def validate_caseID(self, value):
        return value

    def create(self, validated_data):
        dateFrom = validated_data['dateFrom']
        dateTo = validated_data['dateTo']
        category = validated_data['category']
        location_serializer = self.fields['location']
        # case_record_serializer = self.fields['case_record']

        location = location_serializer.create(validated_data['location'])
        # print(validated_data)
        caseID = validated_data['caseID']
        case_record = CaseRecord.objects.get(id=caseID)

        visited_location = VisitedLocation.objects.create(
            dateFrom=dateFrom,
            dateTo=dateTo,
            category=category,
            location=location,
            caseRecord=case_record)

        return visited_location
def location_list(request):
    """
    List all locations, or create a new location.
    """
    if request.method == 'GET':
        locations = Location.objects.all()
        serializer = LocationSerializer(locations, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = LocationSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)
Beispiel #6
0
 def get(self, request):
     user = request.user
     serializer = LocationSerializer(user.favs.all(),
                                     many=True,
                                     context={
                                         "request": request
                                     }).data
     return Response(serializer)
Beispiel #7
0
class UserDetailSerializer(serializers.ModelSerializer):
    location = LocationSerializer()
    institution = InstitutionSerializer()
    language = LanguageSerializer()
    career = CareerSerializer()

    class Meta:
        model = UserDetail
        fields = '__all__'
Beispiel #8
0
    def list(request):
        """List reusable locations."""
        queryset = Location.objects.filter(reusable=True)

        # Check if we don't want residences
        exclude = request.GET.get('exclude_group')
        if exclude is not None:
            queryset = queryset.exclude(group_id=int(exclude))

        return Response(LocationSerializer(queryset, many=True).data)
Beispiel #9
0
class PositionListingDetailSerializer(serializers.ModelSerializer):
    position = PositionTypeSerializer()
    location = LocationSerializer()

    class Meta:
        model = PositionListing
        fields = ['url', "id", "position", "location", "post_date"]
        lookup_field = 'id'
        extra_kwargs = {
            'url': {'lookup_field': 'id'}
        }
Beispiel #10
0
def location_list(request):
    """
    List all locations, or create a new location.
    """
    if request.method == 'GET':
        locations = Location.objects.all()
        serializer = LocationSerializer(locations, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = LocationSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)
Beispiel #11
0
class TaskListTaskSerializer(serializers.ModelSerializer):
    assignee = CustomUserDetailsSerializer(required=False)
    beneficiary = CustomUserDetailsSerializer(required=False)
    task_type = TaskTypeSerializer(required=False)
    status = TaskStatusSerializer(required=False)
    task_interests = InterestSerializer(required=False, many=True)
    party = CustomUserDetailsSerializer(required=False, many=True)
    location = LocationSerializer(required=False)
    pledges = PledgeSerializer(required=False, many=True)
    comments = TaskCommentSerializer(required=False, many=True)

    class Meta:
        model = Task

        fields = (
            'id',
            'assignee',
            'creator',
            'beneficiary',
            'start_date',
            'due_date',
            'status',
            'task_type',
            'pledges',
            'comments',
            'created_at',
            'updated_at',
            'form_data',
            'task_interests',
            'party',
            'beneficiary_rating',
            'assignee_rating',
            'name',
            'location',
            'mobile',
        )

        read_only_fields = (
            'id',
            'created_at',
            'updated_at',
        )
Beispiel #12
0
    def list(self, *args, **kwargs):
        try:
            production = Production.objects.get(slug=kwargs['slug'])
        except Production.DoesNotExist:
            raise Http404

        locations = []

        for location in production.locations.all():
            scenes = location.scene_set.filter(production__slug=kwargs['slug'])
            locations.append({
                "location":
                LocationSerializer(location).data,
                "scenes": [
                    SceneSerializer(s).data for s in location.scene_set.filter(
                        production__slug=kwargs['slug'])
                ]
            })

        return Response(locations)
Beispiel #13
0
    def get(self, request):
        try:
            # All states are queried by pre-fetching(joining) the districts in it
            queryset = list(
                dict.fromkeys(Location.objects.values_list('state',
                                                           flat=True)))
            # The queryset is then serialized to the required format
            serializer = LocationSerializer(queryset, many=True)

            return Response(
                {
                    'success': True,
                    'message': 'Location list successfully fetched',
                    'data': serializer.data,
                },
                status=status.HTTP_200_OK)
        except Exception as e:
            return Response({
                'success': False,
                'error': e.__str__(),
            },
                            status=status.HTTP_403_FORBIDDEN)
Beispiel #14
0
 def list(request):
     """List reusable locations."""
     queryset = Location.objects.filter(reusable=True)
     return Response(LocationSerializer(queryset, many=True).data)
Beispiel #15
0
 def get_locations(self, obj):
     return [
         LocationSerializer().to_representation(l)
         for l in obj.intervention_locations
     ]
    def handle(self, *args, **options):
        exported_data = []
        username = get_env_variable('ELASTIC_USERNAME')
        password = get_env_variable('ELASTIC_PASSWORD')
        index_name = get_env_variable('INDEX_NAME')

        es = Elasticsearch([{
            'host': 'localhost',
            'post': 9200
        }],
                           timeout=30,
                           http_auth=(username, password))

        last_updated = datetime.now() - timedelta(365 * 10)

        with open('/home/ubuntu/vms2/pushToEs_last_updated', 'r') as f:
            rows = csv.reader(f)

            for row in rows:
                last_updated = datetime.strptime(row[0],
                                                 "%Y-%m-%d %H:%M:%S.%f")

        tasks = Task.objects.filter(updated_at__gt=last_updated).exclude(
            task_type__task_type='Retention Survey').order_by(
                'updated_at')[:1000]
        # tasks = Task.objects.filter(updated_at__gt=last_updated).order_by('updated_at')[:2500]

        for task in tasks:
            task_doc = dict(TaskListTaskSerializer(task).data)

            task_doc['timestamp'] = datetime.now()
            task_doc['form_data'] = dict(
                FormDataSerializer(task.form_data).data)
            task_doc['task_status_category'] = dict(
                TaskStatusCategorySerializer(task.status.category).data)

            if task.beneficiary and hasattr(task.beneficiary, 'profile'):
                task_doc['beneficiary'][
                    'date_joined'] = task.beneficiary.date_joined
                task_doc['beneficiary']['profile'] = dict(
                    UserSerializer(task.beneficiary.profile).data)
                task_doc['beneficiary']['notes'] = json.loads(
                    json.dumps(
                        NoteSerializer(
                            task.beneficiary.beneficiary_notes.all(),
                            many=True).data))

            if task.beneficiary and hasattr(task.beneficiary,
                                            'persistent_data'):
                task_doc['beneficiary']['persistent_data'] = dict(
                    PersistentFormDataSerializer(
                        task.beneficiary.persistent_data).data)
                if 'Address' in task_doc['beneficiary']['persistent_data'][
                        'data'].keys():
                    try:
                        location = Location.objects.get(
                            pk=task_doc['beneficiary']['persistent_data']
                            ['data']['Address'])
                        task_doc['beneficiary']['location'] = dict(
                            LocationSerializer(location).data)
                        task_doc['beneficiary']['location'][
                            'geocordinates'] = {}
                        task_doc['beneficiary']['location']['geocordinates'][
                            'lat'] = location.lat
                        task_doc['beneficiary']['location']['geocordinates'][
                            'lon'] = location.lng
                    except:
                        pass

            calls = Call.objects.filter(task=task)
            task_doc['calls'] = []
            for call in calls:
                call_dict = dict(CallSerializer(call).data)
                if call_dict['end']:
                    call_duration = (call.end_time - call.start_time).seconds
                    call_dict['duration'] = call_duration
                    call_dict['caller'] = dict(
                        UserSerializer(call.caller.profile).data)
                    task_doc['calls'].append(call_dict)

            if task.beneficiary:
                task_doc['beneficiary']['tags'] = []

                tags = task.beneficiary.tags.all()
                for tag in tags:
                    task_doc['beneficiary']['tags'].append(
                        dict(LightTagSerializer(tag).data))

                if hasattr(task.beneficiary.profile, 'stage'):
                    task_doc['beneficiary']['stage'] = dict(
                        StageSerializer(task.beneficiary.profile.stage).data)

            if task.assignee and hasattr(task.assignee, 'profile'):
                task_doc['assignee']['profile'] = dict(
                    UserSerializer(task.assignee.profile).data)

            if task.assignee and hasattr(task.assignee, 'profile') and hasattr(
                    task.assignee.profile, 'guild'):
                task_doc['assignee']['guild'] = dict(
                    GuildSerializer(task.assignee.profile.guild).data)

            # print task_doc
            try:
                res = es.index(index=index_name,
                               doc_type='task',
                               id=task.id,
                               body=task_doc)
            except Exception as e:
                if e.error == 'mapper_parsing_exception':
                    print task_doc
                    task_doc['beneficiary']['persistent_data']['data'][
                        'Children'][0]['Date of Birth'] = datetime.strptime(
                            task_doc['beneficiary']['persistent_data']['data']
                            ['Children'][0]['Date of Birth'], '%d/%m/%Y')
                    res = es.index(index='ia',
                                   doc_type='task',
                                   id=task.id,
                                   timestamp=task_doc['timestamp'],
                                   body=task_doc)
                    # print task_doc
                    continue
                last_updated = task.updated_at
                # print task_doc
                break
            last_updated = task.updated_at

        with open('/home/ubuntu/vms2/pushToEs_last_updated', 'w') as f:
            f.write(last_updated.strftime("%Y-%m-%d %H:%M:%S.%f"))
class SearchResultSerializer(serializers.Serializer):
    animals = AnimalSerializer(read_only=True, many=True)
    locations = LocationSerializer(read_only=True, many=True)
    species = SpeciesSerializer(read_only=True, many=True)