|
22 | 22 | from notifications_utils.pdf import pdf_page_count |
23 | 23 | from notifications_utils.s3 import s3download |
24 | 24 | from notifications_utils.template import Template |
| 25 | +from opentelemetry import trace |
| 26 | +from opentelemetry.baggage import set_baggage |
| 27 | +from opentelemetry.context import attach, detach |
25 | 28 | from pypdf.errors import PdfReadError |
26 | 29 | from requests import RequestException |
27 | 30 |
|
@@ -719,69 +722,80 @@ def abort_for_unauthorised_bilingual_letters_or_invalid_options(language: str | |
719 | 722 | def edit_service_template(service_id, template_id, language=None): |
720 | 723 | template = current_service.get_template_with_user_permission_or_403(template_id, current_user) |
721 | 724 |
|
722 | | - if template.template_type not in current_service.available_template_types: |
723 | | - return redirect( |
724 | | - url_for( |
725 | | - ".action_blocked", |
726 | | - service_id=service_id, |
727 | | - notification_type=template.template_type, |
728 | | - return_to="view_template", |
729 | | - template_id=template.id, |
730 | | - ) |
731 | | - ) |
| 725 | + ctx = set_baggage("template_id", str(template_id)) |
| 726 | + token = attach(ctx) |
732 | 727 |
|
733 | | - abort_for_unauthorised_bilingual_letters_or_invalid_options(language, template) |
| 728 | + with trace.get_tracer(__name__).start_as_current_span("edit_service_template") as span: |
| 729 | + try: |
| 730 | + if template.template_type not in current_service.available_template_types: |
| 731 | + return redirect( |
| 732 | + url_for( |
| 733 | + ".action_blocked", |
| 734 | + service_id=service_id, |
| 735 | + notification_type=template.template_type, |
| 736 | + return_to="view_template", |
| 737 | + template_id=template.id, |
| 738 | + ) |
| 739 | + ) |
734 | 740 |
|
735 | | - form = get_template_form(template.template_type, language=language)(**template._template) |
| 741 | + abort_for_unauthorised_bilingual_letters_or_invalid_options(language, template) |
736 | 742 |
|
737 | | - if form.validate_on_submit(): |
738 | | - new_template = get_template( |
739 | | - template._template | form.new_template_data, |
740 | | - current_service, |
741 | | - ) |
742 | | - template_change = template.compare_to(new_template) |
| 743 | + form = get_template_form(template.template_type, language=language)(**template._template) |
743 | 744 |
|
744 | | - if template_change.placeholders_added and not request.form.get("confirm") and current_service.api_keys: |
745 | | - return render_template( |
746 | | - "views/templates/breaking-change.html", |
747 | | - template_change=template_change, |
748 | | - new_template=new_template, |
749 | | - form=form, |
750 | | - ) |
751 | | - try: |
752 | | - service_api_client.update_service_template( |
753 | | - service_id=service_id, |
754 | | - template_id=template_id, |
755 | | - **form.new_template_data, |
756 | | - ) |
757 | | - except HTTPError as e: |
758 | | - if e.status_code == 400: |
759 | | - if "content" in e.message and any("character count greater than" in x for x in e.message["content"]): |
760 | | - form.template_content.errors.extend(e.message["content"]) |
761 | | - elif "content" in e.message and any(x == QR_CODE_TOO_LONG for x in e.message["content"]): |
762 | | - form.template_content.errors.append( |
763 | | - "Cannot create a usable QR code - the link you entered is too long" |
| 745 | + if form.validate_on_submit(): |
| 746 | + new_template = get_template( |
| 747 | + template._template | form.new_template_data, |
| 748 | + current_service, |
| 749 | + ) |
| 750 | + template_change = template.compare_to(new_template) |
| 751 | + |
| 752 | + span.add_event("This is an example span event") |
| 753 | + |
| 754 | + if template_change.placeholders_added and not request.form.get("confirm") and current_service.api_keys: |
| 755 | + return render_template( |
| 756 | + "views/templates/breaking-change.html", |
| 757 | + template_change=template_change, |
| 758 | + new_template=new_template, |
| 759 | + form=form, |
764 | 760 | ) |
| 761 | + try: |
| 762 | + service_api_client.update_service_template( |
| 763 | + service_id=service_id, |
| 764 | + template_id=template_id, |
| 765 | + **form.new_template_data, |
| 766 | + ) |
| 767 | + except HTTPError as e: |
| 768 | + if e.status_code == 400: |
| 769 | + if "content" in e.message and any( |
| 770 | + "character count greater than" in x for x in e.message["content"] |
| 771 | + ): |
| 772 | + form.template_content.errors.extend(e.message["content"]) |
| 773 | + elif "content" in e.message and any(x == QR_CODE_TOO_LONG for x in e.message["content"]): |
| 774 | + form.template_content.errors.append( |
| 775 | + "Cannot create a usable QR code - the link you entered is too long" |
| 776 | + ) |
| 777 | + else: |
| 778 | + raise e |
| 779 | + else: |
| 780 | + raise e |
765 | 781 | else: |
766 | | - raise e |
767 | | - else: |
768 | | - raise e |
769 | | - else: |
770 | | - editing_english_content_in_bilingual_letter = ( |
771 | | - template.template_type == "letter" and template.welsh_page_count and language != "welsh" |
772 | | - ) |
773 | | - return redirect( |
774 | | - url_for( |
775 | | - "main.view_template", |
776 | | - service_id=service_id, |
777 | | - template_id=template_id, |
778 | | - **( |
779 | | - {"_anchor": "first-page-of-english-in-bilingual-letter"} |
780 | | - if editing_english_content_in_bilingual_letter |
781 | | - else {} |
782 | | - ), |
783 | | - ) |
784 | | - ) |
| 782 | + editing_english_content_in_bilingual_letter = ( |
| 783 | + template.template_type == "letter" and template.welsh_page_count and language != "welsh" |
| 784 | + ) |
| 785 | + return redirect( |
| 786 | + url_for( |
| 787 | + "main.view_template", |
| 788 | + service_id=service_id, |
| 789 | + template_id=template_id, |
| 790 | + **( |
| 791 | + {"_anchor": "first-page-of-english-in-bilingual-letter"} |
| 792 | + if editing_english_content_in_bilingual_letter |
| 793 | + else {} |
| 794 | + ), |
| 795 | + ) |
| 796 | + ) |
| 797 | + finally: |
| 798 | + detach(token) |
785 | 799 |
|
786 | 800 | return render_template( |
787 | 801 | f"views/edit-{template.template_type}-template.html", |
|
0 commit comments