Repository: mozmeao/snippets-service

Issue:
Make preview work for right-to-left languages
We used to have this functionality pre-Quantum, curious if it's a lot of work to incorporate our current system. 

<!-- probot = {"419912":{"who":"glogiotatidis","what":"","when":"2020-02-11T09:00:00.000Z"}} -->


Discussion:
This is a bug on the AS side, thus not very simple to fix. Filed bug https://bugzilla.mozilla.org/show_bug.cgi?id=1601880 to track this.

Is this high priority? Should we ask to prioritize fixing for Fx72?

/remind me in 1 week
@glogiotatidis set a reminder for **Dec 13th 2019**
After talking with Andrei it seems that this is only a Preview issue and snippets are displayed as they should in right-to-left browsers.
Andrei from AS will land a fix to allow us to correctly preview right-to-left snippets. Then we will use this to prepare proper preview links for right-to-left languages.

Marked blocked until AS work is done.

/remind me in a month

@glogiotatidis set a reminder for **Jan 10th 2020**
Update from bug
> The URLSearchParams now has a property "dir", which can be set to "rtl" to preview right-to-left orientation


Resolving patch:
diff --git a/snippets/base/migrations/0029_locale_rtl.py b/snippets/base/migrations/0029_locale_rtl.py
new file mode 100644
--- /dev/null
+++ b/snippets/base/migrations/0029_locale_rtl.py
@@ -0,0 +1,18 @@
+# Generated by Django 2.2.6 on 2019-12-16 09:58
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('base', '0028_target_filtr_needs_update'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='locale',
+            name='rtl',
+            field=models.BooleanField(default=False, help_text='Is Right-To-Left language?'),
+        ),
+    ]
diff --git a/snippets/base/models.py b/snippets/base/models.py
--- a/snippets/base/models.py
+++ b/snippets/base/models.py
@@ -1730,6 +1730,10 @@ class Locale(models.Model):
         blank=True, validators=[validators.validate_json_data], default='{}',
         help_text='JSON dictionary with Template fields as keys and localized strings as values.'
     )
+    rtl = models.BooleanField(
+        default=False,
+        help_text='Is Right-To-Left language?'
+    )
 
     def save(self, *args, **kwargs):
         # Make sure that code always starts and ends with `,` and it's always
@@ -2077,7 +2081,8 @@ def get_preview_url(self, dark=False):
             theme = 'dark'
         url = reverse('asr-preview', kwargs={'uuid': self.uuid})
         full_url = urljoin(settings.ADMIN_REDIRECT_URL or settings.SITE_URL, url)
-        return 'about:newtab?theme={}&endpoint={}'.format(theme, full_url)
+        rtl = 'rtl' if self.locale.rtl else 'ltr'
+        return f'about:newtab?theme={theme}&dir={rtl}&endpoint={full_url}'
 
     def get_admin_url(self, full=True):
         # Not using reverse() because the `admin:` namespace is not registered


Test patch:
diff --git a/snippets/base/tests/test_models.py b/snippets/base/tests/test_models.py
--- a/snippets/base/tests/test_models.py
+++ b/snippets/base/tests/test_models.py
@@ -554,7 +554,7 @@ def test_render_preview_only(self):
     @override_settings(SITE_URL='http://example.com')
     def test_get_preview_url(self):
         snippet = ASRSnippetFactory.create()
-        expected_result = 'about:newtab?theme=light&endpoint=http://example.com'
+        expected_result = 'about:newtab?theme=light&dir=ltr&endpoint=http://example.com'
         expected_result += reverse('asr-preview', kwargs={'uuid': snippet.uuid})
         self.assertEqual(snippet.get_preview_url(), expected_result)
 
@@ -562,14 +562,22 @@ def test_get_preview_url(self):
                        ADMIN_REDIRECT_URL='http://admin.example.com')
     def test_get_preview_url_admin(self):
         snippet = ASRSnippetFactory.create()
-        expected_result = 'about:newtab?theme=light&endpoint=http://admin.example.com'
+        expected_result = 'about:newtab?theme=light&dir=ltr&endpoint=http://admin.example.com'
         expected_result += reverse('asr-preview', kwargs={'uuid': snippet.uuid})
         self.assertEqual(snippet.get_preview_url(), expected_result)
 
     @override_settings(SITE_URL='http://example.com')
     def test_get_preview_url_dark(self):
         snippet = ASRSnippetFactory.create()
-        expected_result = 'about:newtab?theme=dark&endpoint=http://example.com'
+        expected_result = 'about:newtab?theme=dark&dir=ltr&endpoint=http://example.com'
+        expected_result += reverse('asr-preview', kwargs={'uuid': snippet.uuid})
+        self.assertEqual(snippet.get_preview_url(dark=True), expected_result)
+
+    @override_settings(SITE_URL='http://example.com')
+    def test_get_preview_url_rtl(self):
+        snippet = ASRSnippetFactory.create()
+        snippet.locale.rtl = True
+        expected_result = 'about:newtab?theme=dark&dir=rtl&endpoint=http://example.com'
         expected_result += reverse('asr-preview', kwargs={'uuid': snippet.uuid})
         self.assertEqual(snippet.get_preview_url(dark=True), expected_result)