Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨(frontend) Template builder #37

Merged
merged 9 commits into from
Apr 17, 2024
12 changes: 11 additions & 1 deletion src/backend/core/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,19 @@ class TemplateSerializer(BaseResourceSerializer):

class Meta:
model = models.Template
fields = ["id", "title", "accesses", "abilities"]
fields = ["id", "title", "code_editor", "accesses", "abilities", "css", "code"]
read_only_fields = ["id", "accesses", "abilities"]

def to_representation(self, instance):
"""
Modify the output of serialization.
"""
representation = super().to_representation(instance)
# Remove 'css' and 'code' from the representation
representation.pop("css", None)
representation.pop("code", None)
return representation


# pylint: disable=abstract-method
class DocumentGenerationSerializer(serializers.Serializer):
Expand Down
1 change: 1 addition & 0 deletions src/backend/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Migration(migrations.Migration):
('updated_at', models.DateTimeField(auto_now=True, help_text='date and time at which a record was last updated', verbose_name='updated on')),
('title', models.CharField(max_length=255, verbose_name='title')),
('description', models.TextField(blank=True, verbose_name='description')),
('code_editor', models.JSONField(blank=True, default=dict, help_text='A JSON object with all the editor information', verbose_name='code editor')),
('code', models.TextField(blank=True, verbose_name='code')),
('css', models.TextField(blank=True, verbose_name='css')),
('is_public', models.BooleanField(default=False, help_text='Whether this template is public for anyone to use.', verbose_name='public')),
Expand Down
8 changes: 8 additions & 0 deletions src/backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def get_abilities(self, user):
"destroy": RoleChoices.OWNER in roles,
"manage_accesses": is_owner_or_admin,
"update": is_owner_or_admin,
"partial_update": is_owner_or_admin,
"retrieve": can_get,
}

Expand Down Expand Up @@ -326,6 +327,12 @@ class Template(BaseModel):

title = models.CharField(_("title"), max_length=255)
description = models.TextField(_("description"), blank=True)
code_editor = models.JSONField(
_("code editor"),
help_text=_("A JSON object with all the editor information"),
blank=True,
default=dict,
)
code = models.TextField(_("code"), blank=True)
css = models.TextField(_("css"), blank=True)
is_public = models.BooleanField(
Expand Down Expand Up @@ -358,6 +365,7 @@ def get_abilities(self, user):
"generate_document": can_get,
"manage_accesses": is_owner_or_admin,
"update": is_owner_or_admin,
"partial_update": is_owner_or_admin,
"retrieve": can_get,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_api_documents_retrieve_anonymous_public():
"abilities": {
"destroy": False,
"manage_accesses": False,
"partial_update": False,
"retrieve": True,
"update": False,
},
Expand Down Expand Up @@ -60,6 +61,7 @@ def test_api_documents_retrieve_authenticated_unrelated_public():
"abilities": {
"destroy": False,
"manage_accesses": False,
"partial_update": False,
"retrieve": True,
"update": False,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ def test_api_templates_retrieve_anonymous_public():
"destroy": False,
"generate_document": True,
"manage_accesses": False,
"partial_update": False,
"retrieve": True,
"update": False,
},
"accesses": [],
"title": template.title,
"code_editor": {},
}


Expand Down Expand Up @@ -62,11 +64,13 @@ def test_api_templates_retrieve_authenticated_unrelated_public():
"destroy": False,
"generate_document": True,
"manage_accesses": False,
"partial_update": False,
"retrieve": True,
"update": False,
},
"accesses": [],
"title": template.title,
"code_editor": {},
}


Expand Down Expand Up @@ -131,6 +135,7 @@ def test_api_templates_retrieve_authenticated_related_direct():
"id": str(template.id),
"title": template.title,
"abilities": template.get_abilities(user),
"code_editor": {},
}


Expand Down Expand Up @@ -244,6 +249,7 @@ def test_api_templates_retrieve_authenticated_related_team_members(
"id": str(template.id),
"title": template.title,
"abilities": template.get_abilities(user),
"code_editor": {},
}


Expand Down Expand Up @@ -339,6 +345,7 @@ def test_api_templates_retrieve_authenticated_related_team_administrators(
"id": str(template.id),
"title": template.title,
"abilities": template.get_abilities(user),
"code_editor": {},
}


Expand Down Expand Up @@ -438,4 +445,5 @@ def test_api_templates_retrieve_authenticated_related_team_owners(
"id": str(template.id),
"title": template.title,
"abilities": template.get_abilities(user),
"code_editor": {},
}
8 changes: 8 additions & 0 deletions src/backend/core/tests/test_models_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_models_documents_get_abilities_anonymous_public():
"retrieve": True,
"update": False,
"manage_accesses": False,
"partial_update": False,
}


Expand All @@ -70,6 +71,7 @@ def test_models_documents_get_abilities_anonymous_not_public():
"retrieve": False,
"update": False,
"manage_accesses": False,
"partial_update": False,
}


Expand All @@ -82,6 +84,7 @@ def test_models_documents_get_abilities_authenticated_public():
"retrieve": True,
"update": False,
"manage_accesses": False,
"partial_update": False,
}


Expand All @@ -94,6 +97,7 @@ def test_models_documents_get_abilities_authenticated_not_public():
"retrieve": False,
"update": False,
"manage_accesses": False,
"partial_update": False,
}


Expand All @@ -107,6 +111,7 @@ def test_models_documents_get_abilities_owner():
"retrieve": True,
"update": True,
"manage_accesses": True,
"partial_update": True,
}


Expand All @@ -119,6 +124,7 @@ def test_models_documents_get_abilities_administrator():
"retrieve": True,
"update": True,
"manage_accesses": True,
"partial_update": True,
}


Expand All @@ -134,6 +140,7 @@ def test_models_documents_get_abilities_member_user(django_assert_num_queries):
"retrieve": True,
"update": False,
"manage_accesses": False,
"partial_update": False,
}


Expand All @@ -150,4 +157,5 @@ def test_models_documents_get_abilities_preset_role(django_assert_num_queries):
"retrieve": True,
"update": False,
"manage_accesses": False,
"partial_update": False,
}
14 changes: 14 additions & 0 deletions src/backend/core/tests/test_models_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_models_templates_get_abilities_anonymous_public():
"retrieve": True,
"update": False,
"manage_accesses": False,
"partial_update": False,
"generate_document": True,
}

Expand All @@ -71,6 +72,7 @@ def test_models_templates_get_abilities_anonymous_not_public():
"retrieve": False,
"update": False,
"manage_accesses": False,
"partial_update": False,
"generate_document": False,
}

Expand All @@ -84,6 +86,7 @@ def test_models_templates_get_abilities_authenticated_public():
"retrieve": True,
"update": False,
"manage_accesses": False,
"partial_update": False,
"generate_document": True,
}

Expand All @@ -97,6 +100,7 @@ def test_models_templates_get_abilities_authenticated_not_public():
"retrieve": False,
"update": False,
"manage_accesses": False,
"partial_update": False,
"generate_document": False,
}

Expand All @@ -111,6 +115,7 @@ def test_models_templates_get_abilities_owner():
"retrieve": True,
"update": True,
"manage_accesses": True,
"partial_update": True,
"generate_document": True,
}

Expand All @@ -124,6 +129,7 @@ def test_models_templates_get_abilities_administrator():
"retrieve": True,
"update": True,
"manage_accesses": True,
"partial_update": True,
"generate_document": True,
}

Expand All @@ -140,6 +146,7 @@ def test_models_templates_get_abilities_member_user(django_assert_num_queries):
"retrieve": True,
"update": False,
"manage_accesses": False,
"partial_update": False,
"generate_document": True,
}

Expand All @@ -157,5 +164,12 @@ def test_models_templates_get_abilities_preset_role(django_assert_num_queries):
"retrieve": True,
"update": False,
"manage_accesses": False,
"partial_update": False,
"generate_document": True,
}


def test_models_templates_get_code_editor():
"""Check code_editor in the template model"""
template = factories.TemplateFactory(code_editor={"test": "ok"})
assert template.code_editor == {"test": "ok"}
29 changes: 29 additions & 0 deletions src/frontend/apps/e2e/__tests__/app-impress/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ export const createPad = async (
return randomPads;
};

export const createTemplate = async (
page: Page,
templateName: string,
browserName: string,
length: number,
) => {
const menu = page.locator('menu').first();
await menu.getByLabel(`Template button`).click();

const panel = page.getByLabel('Templates panel').first();
const buttonCreate = page.getByRole('button', {
name: 'Create the template',
});

const randomTemplates = randomName(templateName, browserName, length);

for (let i = 0; i < randomTemplates.length; i++) {
await panel.getByRole('button', { name: 'Add a template' }).click();
await page.getByText('Template name').fill(randomTemplates[i]);
await expect(buttonCreate).toBeEnabled();
await buttonCreate.click();
await expect(
panel.locator('li').getByText(randomTemplates[i]),
).toBeVisible();
}

return randomTemplates;
};

export const addNewMember = async (
page: Page,
index: number,
Expand Down
1 change: 1 addition & 0 deletions src/frontend/apps/e2e/__tests__/app-impress/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ test.beforeEach(async ({ page, browserName }) => {
test.describe('Menu', () => {
const menuItems = [
{ name: 'Search', isDefault: true },
{ name: 'Template', isDefault: false },
{ name: 'Favorite', isDefault: false },
{ name: 'Recent', isDefault: false },
{ name: 'Contacts', isDefault: false },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ test.describe('Pad Editor', () => {
await expect(page.getByText('[test markdown]')).toBeVisible();

await page.getByText('[test markdown]').dblclick();
await page
.getByRole('button', {
name: 'M',
})
.click();
await page.locator('button[data-test="convertMarkdown"]').click();

await expect(page.getByText('[test markdown]')).toBeHidden();
await expect(
Expand Down
Loading
Loading