Browse Source

Fix handling of multiple INSEE on same postal code

Gautier P 9 months ago
parent
commit
67da9a055e

+ 55
- 44
flatisfy/web/js_src/store/getters.js View File

@@ -1,77 +1,88 @@
1 1
 import { findFlatGPS, costFilter } from '../tools'
2 2
 
3 3
 export default {
4
-    allFlats: state => state.flats,
4
+    allFlats: (state) => state.flats,
5 5
 
6
-    flat: (state, getters) => id => state.flats.find(flat => flat.id === id),
6
+    flat: (state, getters) => (id) =>
7
+        state.flats.find((flat) => flat.id === id),
7 8
 
8
-    isLoading: state => state.loading > 0,
9
+    isLoading: (state) => state.loading > 0,
9 10
 
10
-    postalCodesFlatsBuckets: (state, getters) => filter => {
11
-        const postalCodeBuckets = {}
11
+    inseeCodesFlatsBuckets: (state, getters) => (filter) => {
12
+        const buckets = {};
12 13
 
13
-        state.flats.forEach(flat => {
14
+        state.flats.forEach((flat) => {
14 15
             if (!filter || filter(flat)) {
15
-                const postalCode = flat.flatisfy_postal_code.postal_code
16
-                if (!postalCodeBuckets[postalCode]) {
17
-                    postalCodeBuckets[postalCode] = {
18
-                        'name': flat.flatisfy_postal_code.name,
19
-                        'flats': []
20
-                    }
16
+                const insee = flat.flatisfy_postal_code.insee_code;
17
+                if (!buckets[insee]) {
18
+                    buckets[insee] = {
19
+                        name: flat.flatisfy_postal_code.name,
20
+                        flats: [],
21
+                    };
21 22
                 }
22
-                postalCodeBuckets[postalCode].flats.push(flat)
23
+                buckets[insee].flats.push(flat);
23 24
             }
24
-        })
25
+        });
25 26
 
26
-        return postalCodeBuckets
27
+        return buckets;
27 28
     },
28 29
 
29 30
     flatsMarkers: (state, getters) => (router, filter) => {
30
-        const markers = []
31
-        state.flats.forEach(flat => {
31
+        const markers = [];
32
+        state.flats.forEach((flat) => {
32 33
             if (filter && filter(flat)) {
33
-                const gps = findFlatGPS(flat)
34
+                const gps = findFlatGPS(flat);
34 35
 
35 36
                 if (gps) {
36 37
                     const previousMarker = markers.find(
37
-                        marker => marker.gps[0] === gps[0] && marker.gps[1] === gps[1]
38
-                    )
38
+                        (marker) =>
39
+                            marker.gps[0] === gps[0] && marker.gps[1] === gps[1]
40
+                    );
39 41
                     if (previousMarker) {
40
-                      // randomize position a bit
41
-                      // gps[0] += (Math.random() - 0.5) / 500
42
-                      // gps[1] += (Math.random() - 0.5) / 500
42
+                        // randomize position a bit
43
+                        // gps[0] += (Math.random() - 0.5) / 500
44
+                        // gps[1] += (Math.random() - 0.5) / 500
43 45
                     }
44
-                    const href = router.resolve({ name: 'details', params: { id: flat.id }}).href
46
+                    const href = router.resolve({
47
+                        name: "details",
48
+                        params: { id: flat.id },
49
+                    }).href;
45 50
                     const cost = flat.cost
46 51
                         ? costFilter(flat.cost, flat.currency)
47
-                        : ''
52
+                        : "";
48 53
                     markers.push({
49
-                        'title': '',
50
-                        'content': '<a href="' + href + '">' + flat.title + '</a>' + cost,
51
-                        'gps': gps,
52
-                        'flat_id': flat.id
53
-                    })
54
+                        title: "",
55
+                        content:
56
+                            '<a href="' +
57
+                            href +
58
+                            '">' +
59
+                            flat.title +
60
+                            "</a>" +
61
+                            cost,
62
+                        gps: gps,
63
+                        flat_id: flat.id,
64
+                    });
54 65
                 }
55 66
             }
56
-        })
67
+        });
57 68
 
58
-        return markers
69
+        return markers;
59 70
     },
60 71
 
61
-    allTimeToPlaces: state => {
62
-        const places = {}
63
-        Object.keys(state.timeToPlaces).forEach(constraint => {
64
-            const constraintTimeToPlaces = state.timeToPlaces[constraint]
65
-            Object.keys(constraintTimeToPlaces).forEach(name => {
66
-                places[name] = constraintTimeToPlaces[name]
67
-            })
68
-        })
69
-        return places
72
+    allTimeToPlaces: (state) => {
73
+        const places = {};
74
+        Object.keys(state.timeToPlaces).forEach((constraint) => {
75
+            const constraintTimeToPlaces = state.timeToPlaces[constraint];
76
+            Object.keys(constraintTimeToPlaces).forEach((name) => {
77
+                places[name] = constraintTimeToPlaces[name];
78
+            });
79
+        });
80
+        return places;
70 81
     },
71 82
 
72 83
     timeToPlaces: (state, getters) => (constraintName) => {
73
-        return state.timeToPlaces[constraintName]
84
+        return state.timeToPlaces[constraintName];
74 85
     },
75 86
 
76
-    metadata: state => state.metadata
77
-}
87
+    metadata: (state) => state.metadata,
88
+};

+ 9
- 9
flatisfy/web/js_src/views/home.vue View File

@@ -15,16 +15,16 @@
15 15
             </label>
16 16
             </h2>
17 17
 
18
-            <template v-if="Object.keys(postalCodesFlatsBuckets).length > 0">
19
-                <template v-for="(postal_code_data, postal_code) in postalCodesFlatsBuckets">
18
+            <template v-if="Object.keys(inseeCodesFlatsBuckets).length > 0">
19
+                <template v-for="(insee_code_data, insee_code) in inseeCodesFlatsBuckets">
20 20
                     <h3>
21
-                        {{ postal_code_data.name || $t('common.Unknown') }}
22
-                        <span v-if="postal_code !== 'undefined'">
23
-                            ({{ postal_code }})
21
+                        {{ insee_code_data.name || $t('common.Unknown') }}
22
+                        <span v-if="insee_code !== 'undefined'">
23
+                            ({{ insee_code }})
24 24
                         </span>
25
-                        - {{ postal_code_data.flats.length }} {{ $tc("common.flats", postal_code_data.flats.length) }}
25
+                        - {{ insee_code_data.flats.length }} {{ $tc("common.flats", insee_code_data.flats.length) }}
26 26
                     </h3>
27
-                    <FlatsTable :flats="postal_code_data.flats" :key="postal_code"></FlatsTable>
27
+                    <FlatsTable :flats="insee_code_data.flats" :key="insee_code"></FlatsTable>
28 28
                 </template>
29 29
             </template>
30 30
             <template v-else-if="isLoading">
@@ -83,8 +83,8 @@ export default {
83 83
     },
84 84
 
85 85
     computed: {
86
-        postalCodesFlatsBuckets () {
87
-            return this.$store.getters.postalCodesFlatsBuckets(flat =>
86
+        inseeCodesFlatsBuckets () {
87
+            return this.$store.getters.inseeCodesFlatsBuckets(flat =>
88 88
                 flat.status === 'new' &&
89 89
                 (this.showExpiredFlats || !flat.is_expired)
90 90
             )

+ 6
- 6
flatisfy/web/js_src/views/search.vue View File

@@ -12,10 +12,10 @@
12 12
         <template v-if="isLoading">
13 13
             <p>{{ $t("common.loading") }}</p>
14 14
         </template>
15
-        <template v-else-if="Object.keys(postalCodesFlatsBuckets).length > 0">
16
-            <template v-for="(postal_code_data, postal_code) in postalCodesFlatsBuckets">
17
-                <h3>{{ postal_code_data.name }} ({{ postal_code }}) - {{ postal_code_data.flats.length }} {{ $tc("common.flats", postal_code_data.flats.length) }}</h3>
18
-                <FlatsTable :flats="postal_code_data.flats"></FlatsTable>
15
+        <template v-else-if="Object.keys(inseeCodesFlatsBuckets).length > 0">
16
+            <template v-for="(insee_code_data, insee_code) in inseeCodesFlatsBuckets">
17
+                <h3>{{ insee_code_data.name }} ({{ insee_code }}) - {{ insee_code_data.flats.length }} {{ $tc("common.flats", insee_code_data.flats.length) }}</h3>
18
+                <FlatsTable :flats="insee_code_data.flats"></FlatsTable>
19 19
             </template>
20 20
         </template>
21 21
         <template v-else>
@@ -51,12 +51,12 @@ export default {
51 51
     },
52 52
 
53 53
     computed: {
54
-        postalCodesFlatsBuckets () {
54
+        inseeCodesFlatsBuckets () {
55 55
             if (!this.$route.query.query || this.loading) {
56 56
                 return {}
57 57
             }
58 58
 
59
-            return this.$store.getters.postalCodesFlatsBuckets(
59
+            return this.$store.getters.inseeCodesFlatsBuckets(
60 60
                 flat => flat.status !== 'duplicate' && flat.status !== 'ignored' && flat.status !== 'user_deleted'
61 61
             )
62 62
         },

+ 6
- 6
flatisfy/web/js_src/views/status.vue View File

@@ -17,11 +17,11 @@
17 17
         <template v-if="isLoading">
18 18
             <p>{{ $t("common.loading") }}</p>
19 19
         </template>
20
-        <template v-else-if="Object.keys(postalCodesFlatsBuckets).length">
21
-            <template v-for="(postal_code_data, postal_code) in postalCodesFlatsBuckets">
22
-                <h3>{{ postal_code_data.name }} ({{ postal_code }}) - {{ postal_code_data.flats.length }} {{ $tc("common.flats", postal_code_data.flats.length) }}</h3>
20
+        <template v-else-if="Object.keys(inseeCodesFlatsBuckets).length">
21
+            <template v-for="(insee_code_data, insee_code) in inseeCodesFlatsBuckets">
22
+                <h3>{{ insee_code_data.name }} ({{ insee_code }}) - {{ insee_code_data.flats.length }} {{ $tc("common.flats", insee_code_data.flats.length) }}</h3>
23 23
                 <FlatsTable
24
-                    :flats="postal_code_data.flats"
24
+                    :flats="insee_code_data.flats"
25 25
                     :showNotationColumn="$route.params.status === 'followed'"
26 26
                     :showNotes="$route.params.status === 'followed'"
27 27
                     :initialSortBy="$route.params.status === 'followed' ? 'notation' : undefined"
@@ -81,8 +81,8 @@ export default {
81 81
     },
82 82
 
83 83
     computed: {
84
-        postalCodesFlatsBuckets () {
85
-            return this.$store.getters.postalCodesFlatsBuckets(flat => flat.status === this.$route.params.status)
84
+        inseeCodesFlatsBuckets () {
85
+            return this.$store.getters.inseeCodesFlatsBuckets(flat => flat.status === this.$route.params.status)
86 86
         },
87 87
         title () {
88 88
             return 'Flatisfy - ' + capitalize(this.$t('status.' + this.$route.params.status))

+ 9
- 5
flatisfy/web/routes/api.py View File

@@ -7,6 +7,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera
7 7
 import datetime
8 8
 import itertools
9 9
 import json
10
+import logging
10 11
 import re
11 12
 import os
12 13
 
@@ -15,6 +16,7 @@ import vobject
15 16
 
16 17
 import flatisfy.data
17 18
 from flatisfy.models import flat as flat_model
19
+from flatisfy.models import postal_code
18 20
 from flatisfy.models.postal_code import PostalCode
19 21
 from flatisfy import cmds
20 22
 
@@ -106,15 +108,17 @@ def _serialize_flat(flat, config):
106 108
         postal_codes[constraint_name] = flatisfy.data.load_data(PostalCode, constraint, config)
107 109
 
108 110
     try:
109
-        assert flat["flatisfy_postal_code"]
111
+        assert flat["flatisfy_position"]
110 112
 
113
+        lat = flat["flatisfy_position"]["lat"]
114
+        lng = flat["flatisfy_position"]["lng"]
111 115
         postal_code_data = next(
112
-            x
113
-            for x in postal_codes.get(flat["flatisfy_constraint"], [])
114
-            if x.postal_code == flat["flatisfy_postal_code"]
116
+            x for x in postal_codes.get(flat["flatisfy_constraint"], []) if x.lat == lat and x.lng == lng
115 117
         )
118
+        logging.warn(f"{postal_code_data.name}, {lat}, {lng}")
116 119
         flat["flatisfy_postal_code"] = {
117
-            "postal_code": flat["flatisfy_postal_code"],
120
+            "postal_code": postal_code_data.postal_code,
121
+            "insee_code": postal_code_data.insee_code,
118 122
             "name": postal_code_data.name,
119 123
             "gps": (postal_code_data.lat, postal_code_data.lng),
120 124
         }

+ 1
- 1
start.sh View File

@@ -7,7 +7,7 @@ function clean_up {
7 7
     exit
8 8
 }
9 9
 
10
-python -m flatisfy serve --config config.json &
10
+python -m flatisfy serve --config config.json -v &
11 11
 SERVE_PID=$!
12 12
 
13 13
 yarn watch:dev &