Repository: liqd/a4-meinberlin

Issue:
B-Plan API: tile picture not transferred from Imperia to mB; bplan not published
**URL:** see below
**user:** bplan initiator via imperia API
**expected behaviour:** the tile picture entered via Imperia is shown on mB, the plan is published and not only saved as draft
**behaviour:** sometimes the picture is not shown, sometimes the plan does not get automatically published
**important screensize:**
**device & browser:** 
**Comment/Question:** it does not happen always but sometimes with different districts involved. Prio set as high as we need to find a solution but not release right away as there is a workaround.

list of bplans that recently had problems (as far as I can read from mails as I wasn't always included directly into the support):
https://mein.berlin.de/django-admin/meinberlin_bplan/bplan/1535 (May 2022, not published)
https://mein.berlin.de/django-admin/meinberlin_bplan/bplan/1488/change/ (March 22, not published)
https://mein.berlin.de/django-admin/meinberlin_bplan/bplan/1476/change/ (March 22, picture not transferred)
https://mein.berlin.de/django-admin/meinberlin_bplan/bplan/1502/change/ (April 22, not published, picture not transferred)
https://mein.berlin.de/django-admin/meinberlin_bplan/bplan/1449/change/ (Feb 22, picture not transferred)


Regarding 1449, we have already been in contact with BO, they write the following: 

beim übermitteln der Daten aus dem Imperia-System bekommen wir ein
  "HTTP/2 400"

Wäre schön wenn wir bei diesen 400 immer auch gleich eine Error-Meldung mit bekommen könnten, das ist aber immer nur ein schwarzes Loch.

Folgender POST-Request schlägt fehl

{"name":"Bebauungsplan XIV-3-1 (\u201eNeuk\u00f6llnische Allee \/ Schmalenbachstra\u00dfe\u201c)","url":"https:\/\/[www.berlin.de](http://www.berlin.de/)\/ba-neukoelln\/politik-und-verwaltung\/aemter\/stadtentwicklungsamt\/stadtplanung\/bebauungsplaene\/bebauungsplan-fuer-meinberlin\/bebauungsplan.1176597.php","office_worker_email":"[stadtplanung@bezirksamt-neukoelln.de](mailto:stadtplanung@bezirksamt-neukoelln.de)","start_date":"2022-02-21 00:00","end_date":"2022-03-11 23:59","description":"Sicherung und Stabilisierung eines Gewerbegebietes durch den Ausschluss unerw\u00fcnschter Einzelnutzungen.","identifier":"XIV-3-1","is_draft":"False","image_url":"https:\/\/[www.berlin.de](http://www.berlin.de/)\/imgscaler\/r9Iz3AbUfqVDjoWe7B-GLwXcCWHjBVGlMLWfoeyqvwc\/meinberlin\/L3N5czExLXByb2QvYmEtbmV1a29lbGxuL3BvbGl0aWstdW5kLXZlcndhbHR1bmcvYWVtdGVyL3N0YWR0ZW50d2lja2x1bmdzYW10L3N0YWR0cGxhbnVuZy9iZWJhdXVuZ3NwbGFlbmUvYmViYXV1bmdzcGxhbi1mdWVyLW1laW5iZXJsaW4vdW50ZXJsYWdlbi9rYXJ0ZW4tdW5kLXBsYWVuZS91cGxvYWRfXzgzZWIwZjA4OGNjZmEwMTUxNjk0YTcyODQzZWJjYjA1X3hpdi0zLTFfMTAwMDB1ZWJlcnNpY2h0LmpwZw.jpg","image_copyright":"Bezirksamt Neuk\u00f6lln von Berlin"}


Wahrscheinlich ist es die "image_url"
Mit "\\" sind es 408 Zeichen
Ohne "\\" sind es 402 Zeichen

In der API-Beschreibung auf https://github.com/liqd/a4-meinberlin/blob/main/docs/bplan_api.md ist keine Pfad-Längenbeschränkung dokumentiert.
Die Bildgröße ist korrekt.

https://www.berlin.de/imgscaler/r9Iz3AbUfqVDjoWe7B-GLwXcCWHjBVGlMLWfoeyqvwc/meinberlin/L3N5czExLXByb2QvYmEtbmV1a29lbGxuL3BvbGl0aWstdW5kLXZlcndhbHR1bmcvYWVtdGVyL3N0YWR0ZW50d2lja2x1bmdzYW10L3N0YWR0cGxhbnVuZy9iZWJhdXVuZ3NwbGFlbmUvYmViYXV1bmdzcGxhbi1mdWVyLW1laW5iZXJsaW4vdW50ZXJsYWdlbi9rYXJ0ZW4tdW5kLXBsYWVuZS91cGxvYWRfXzgzZWIwZjA4OGNjZmEwMTUxNjk0YTcyODQzZWJjYjA1X3hpdi0zLTFfMTAwMDB1ZWJlcnNpY2h0LmpwZw.jpg
Bildgröße: 800x600  (ist Y als die Minimalgröße 500x300)
Bildgröße <10MB ist a cuh erfüllt: 124332 Bytes


Bitte überprüfen, ob es an image_url liegt
und bitte das das Feld bis 512 Zeichen Länge erlauben


Discussion:
It is right, it is the image url, but I get an error message when I post the exact same content:

```
curl -d '{"name":"Bebauungsplan XIV-3-1 (\u201eNeuk\u00f6llnische Allee / Schmalenbachstra\u00dfe\u201c)",
"url":"https://www.berlin.de/ba-neukoelln/politik-und-verwaltung/aemter/stadtentwicklungsamt/stadtplanung/bebauungsplaene/bebauungsplan-fuer-meinberlin/bebauungsplan.1176597.php",
"office_worker_email":"stadtplanung@bezirksamt-neukoelln.de","start_date":"2022-02-21 00:00",
"end_date":"2022-03-11 23:59",
"description":"Sicherung und Stabilisierung eines Gewerbegebietes durch den Ausschluss unerw\u00fcnschter Einzelnutzungen.",
"identifier":"XIV-3-1","is_draft":"False",
"image_url":"https://www.berlin.de/imgscaler/r9Iz3AbUfqVDjoWe7B-GLwXcCWHjBVGlMLWfoeyqvwc/meinberlin/L3N5czExLXByb2QvYmEtbmV1a29lbGxuL3BvbGl0aWstdW5kLXZlcndhbHR1bmcvYWVtdGVyL3N0YWR0ZW50d2lja2x1bmdzYW10L3N0YWR0cGxhbnVuZy9iZWJhdXVuZ3NwbGFlbmUvYmViYXV1bmdzcGxhbi1mdWVyLW1laW5iZXJsaW4vdW50ZXJsYWdlbi9rYXJ0ZW4tdW5kLXBsYWVuZS91cGxvYWRfXzgzZWIwZjA4OGNjZmEwMTUxNjk0YTcyODQzZWJjYjA1X3hpdi0zLTFfMTAwMDB1ZWJlcnNpY2h0LmpwZw.jpg",
"image_copyright":"Bezirksamt Neuk\u00f6lln von Berlin"}'
 -H "Content-Type: application/json" -X POST http://localhost:8003/api/organisations/1/bplan/ --user 'example_user@liqd.net':'password'

{"image_url":["Stelle sicher, dass dieses Feld nicht mehr als 300 Zeichen lang ist."]}
```

Same if I post to our dev server or do an PATCH (update) instead of POST.
So, we did tackle that and give the correct error message. I don't know what else to do? I mean, we could up that as proposed, but that would still mean, that imperia does not get or display the error message, and that for longer urls it still fails and we get the same question again.

I don't know about the draft state and how that happens. Would be good to also know what they posted. When you add a bplan without the is_draft paremeter, that should be false and the bplan is published. When they added the bplan with that parameter and then wanted to publish later, I can only guess that sth. else is going wrong there and with that the is_draft is also not updated.
@fuzzylogic2000 we had another bplan yesterday that has not been published via API but was there as draft and was manually published via mB (Bebauungsplan 1-112a). May be a different issue? 
I don't know. What exactly did they post?
we have these two problems as explained above: the picture not being shown (we are about to solve this) and that the Bplans are not published via API although published via Imperia. If you technically don't see a connection I will talk again to Senatskanzlei about it as they usually supported in these cases and try to figure our more.
I don't know if they are connected and can only say that when I get more info. Did they add the bplan as draft via imperia and then in a second step add an image and tried to publish it? Then they are most likely connected. 
Or did they create a bplan as published in the first place and then it was created, but as draft even though, they did not set that? That would be very weird.
Could you find out, what exactly they tried? Like the first and second step or if it was all one step, then it's maybe already easier to say, what might be the problem. 

Resolving patch:
diff --git a/meinberlin/apps/bplan/serializers.py b/meinberlin/apps/bplan/serializers.py
--- a/meinberlin/apps/bplan/serializers.py
+++ b/meinberlin/apps/bplan/serializers.py
@@ -39,15 +39,15 @@ class BplanSerializer(serializers.ModelSerializer):
     image_url = serializers.URLField(
         required=False,
         write_only=True,
-        max_length=(project_models.Project._meta.
-                    get_field('tile_image').max_length))
+    )
     image_copyright = serializers.CharField(
         required=False,
         write_only=True,
         source='tile_image_copyright',
         allow_blank=True,
         max_length=(project_models.Project._meta.
-                    get_field('tile_image_copyright').max_length))
+                    get_field('tile_image_copyright').max_length),
+    )
     embed_code = serializers.SerializerMethodField()
 
     class Meta:
@@ -196,9 +196,7 @@ def _generate_image_filename(self, url_path, file):
             file.seek(0)
             extension = imghdr.what(file) or 'jpeg'
 
-        basename = posixpath.basename(root_path)
-        if not basename:
-            basename = 'bplan'
+        basename = 'bplan_%s' % (timezone.now().strftime('%Y%m%dT%H%M%S'))
 
         dirname = datetime.datetime.now().strftime(self._image_upload_to)
         filename = posixpath.join(dirname, basename + '.' + extension)


Test patch: