You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.9 KiB

  1. # Generated by Django 2.2.1 on 2019-05-29 20:37
  2. import json
  3. from django.db import migrations
  4. def normalize_webhook_values(apps, schema_editor):
  5. Channel = apps.get_model("api", "Channel")
  6. for ch in Channel.objects.filter(kind="webhook").only("value"):
  7. # The old format of url_down, url_up, post_data separated by newlines:
  8. if not ch.value.startswith("{"):
  9. parts = ch.value.split("\n")
  10. url_down = parts[0]
  11. url_up = parts[1] if len(parts) > 1 else ""
  12. post_data = parts[2] if len(parts) > 2 else ""
  13. ch.value = json.dumps(
  14. {
  15. "method_down": "POST" if post_data else "GET",
  16. "url_down": url_down,
  17. "body_down": post_data,
  18. "headers_down": {},
  19. "method_up": "POST" if post_data else "GET",
  20. "url_up": url_up,
  21. "body_up": post_data,
  22. "headers_up": {},
  23. }
  24. )
  25. ch.save()
  26. continue
  27. doc = json.loads(ch.value)
  28. # Legacy "post_data" in doc -- use the legacy fields
  29. if "post_data" in doc:
  30. ch.value = json.dumps(
  31. {
  32. "method_down": "POST" if doc["post_data"] else "GET",
  33. "url_down": doc["url_down"],
  34. "body_down": doc["post_data"],
  35. "headers_down": doc["headers"],
  36. "method_up": "POST" if doc["post_data"] else "GET",
  37. "url_up": doc["url_up"],
  38. "body_up": doc["post_data"],
  39. "headers_up": doc["headers"],
  40. }
  41. )
  42. ch.save()
  43. continue
  44. class Migration(migrations.Migration):
  45. dependencies = [("api", "0060_tokenbucket")]
  46. operations = [
  47. migrations.RunPython(normalize_webhook_values, migrations.RunPython.noop)
  48. ]