| # Copyright 2014 the Melange authors. |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """Seeder functions for email model.""" |
| |
| from melange.models import email as email_model |
| |
| |
| TEST_SUBJECT = 'Test Subject' |
| |
| _TEST_TEMPLATE_PATH = 'seeder/email_template.html' |
| |
| _TEST_RECIPIENT_DATA = ( |
| ('test1@example.com', 'Test 1'), |
| ('test2@example.com', 'Test 2'), |
| ('test3@example.com', 'Test 3')) |
| |
| _TEST_SENDER = 'Test Sender' |
| |
| _TEST_TEMPLATE_STRING = """ |
| Hello {{ recipient_name }}! |
| |
| This is test content of email template which is defined by a raw string. |
| |
| Best, |
| {{ sender_name }} |
| """ |
| |
| |
| def seedEmailTemplate(template_type, **kwargs): |
| """Seeds a new email template. |
| |
| Args: |
| template_type: email_model.TemplateType of the seeded email template. |
| |
| Returns: |
| The newly seeder email template entity. |
| """ |
| recipients = [] |
| for item in _TEST_RECIPIENT_DATA: |
| context = { |
| 'recipient_name': item[1], |
| 'sender_name': _TEST_SENDER, |
| } |
| recipients.append(email_model.Recipient(email=item[0], context=context)) |
| |
| properties = { |
| 'recipients': recipients, |
| 'subject': TEST_SUBJECT, |
| 'template_type': template_type, |
| } |
| if template_type == email_model.TemplateType.PATH: |
| properties['template_path'] = _TEST_TEMPLATE_PATH |
| else: |
| properties['template_string'] = _TEST_TEMPLATE_STRING |
| |
| properties.update(**kwargs) |
| |
| email_template = email_model.EmailTemplate(**properties) |
| email_template.put() |
| return email_template |