class VideoPlayer(blocks.StructBlock): video_url = blocks.RegexBlock( label='YouTube Embed URL', default='https://www.youtube.com/embed/', required=True, regex=r'^https:\/\/www\.youtube\.com\/embed\/.+$', error_messages={ 'required': 'The YouTube URL field is required for video players.', 'invalid': "The YouTube URL is in the wrong format. " "You must use the embed URL " "(https://www.youtube.com/embed/video_id), " "which can be obtained by clicking Share > Embed " "on the YouTube video page.", } ) class Meta: icon = 'media' template = '_includes/organisms/video-player.html'
class HTMLBlock(blocks.StructBlock): html_url = blocks.RegexBlock( label='Source URL', default='', required=True, regex=r'^https://(s3.amazonaws.com/)?files.consumerfinance.gov/.+$', error_messages={ 'required': 'The HTML URL field is required for rendering raw ' 'HTML from a remote source.', 'invalid': 'The URL is invalid or not allowed. ', }) def render(self, value, context=None): resp = requests.get(value['html_url'], timeout=5) resp.raise_for_status() return mark_safe(resp.content) class Meta: label = 'HTML Block' icon = 'code'
class ConferenceRegistrationForm(AbstractFormBlock): govdelivery_code = blocks.CharBlock( label='GovDelivery code', help_text=( 'Conference registrants will be subscribed to this GovDelivery ' 'topic.')) govdelivery_question_id = blocks.RegexBlock( required=False, regex=r'^\d{5,}$', error_messages={ 'invalid': 'GovDelivery question ID must be 5 digits.' }, label='GovDelivery question ID', help_text=mark_safe( 'Enter the ID of the question in GovDelivery that is being used ' 'to track registration for this conference. It is the number in ' 'the question URL, e.g., the <code>12345</code> in ' '<code>https://admin.govdelivery.com/questions/12345/edit</code>.') ) govdelivery_answer_id = blocks.RegexBlock( required=False, regex=r'^\d{5,}$', error_messages={'invalid': 'GovDelivery answer ID must be 5 digits.'}, label='GovDelivery answer ID', help_text=mark_safe( 'Enter the ID of the affirmative answer for the above question. ' 'To find it, right-click on the answer in the listing on a page ' 'like <code>https://admin.govdelivery.com/questions/12345/answers' '</code>, inspect the element, and look around in the source for ' 'a five-digit ID associated with that answer. <strong>Required ' 'if Govdelivery question ID is set.</strong>')) capacity = blocks.IntegerBlock(help_text=( 'Enter the (physical) conference attendance limit as a number.')) success_message = blocks.RichTextBlock(help_text=( 'Enter a message that will be shown on successful registration.')) at_capacity_message = blocks.RichTextBlock(help_text=( 'Enter a message that will be shown when the event is at capacity.')) failure_message = blocks.RichTextBlock( help_text=('Enter a message that will be shown if the GovDelivery ' 'subscription fails.')) def clean(self, value): cleaned = super(ConferenceRegistrationForm, self).clean(value) question = cleaned.get('govdelivery_question_id') answer = cleaned.get('govdelivery_answer_id') # Question and answer values must both exist or neither exist if question and not answer: raise ValidationError( 'Validation error in Conference Registration Form: ' 'GovDelivery question ID requires answer ID, and vice versa.', params={ 'govdelivery_answer_id': ErrorList( ['Required if a GovDelivery question ID is entered.']) }) if answer and not question: raise ValidationError( 'Validation error in Conference Registration Form: ' 'GovDelivery question ID requires answer ID, and vice versa.', params={ 'govdelivery_question_id': ErrorList( ['Required if a GovDelivery answer ID is entered.']) }) return cleaned class Meta: handler = 'data_research.handlers.ConferenceRegistrationHandler' template = 'data_research/conference-registration-form.html'
class RelativeURLBlock(AbstractPageBlock): link = blocks.RegexBlock(regex=URL_REGEX, error_mesage={ 'invalid': "Not a relative URL" })