Skip to content

Commit

Permalink
Api models (#1)
Browse files Browse the repository at this point in the history
* Start api models for contribution agreement (#82)

* api models for contribution

* change import sorting and format

* fixture data for testing

* change model fields

* test string parsing

* Migrate models

* fixture data for testing

* api models and tests files

* fix import statements

* fix code format

* Fix static types

* fix linting test file format

* import fix

* Check static code

* import sort

* update DateTime Field

* Tests for models

* Repository testing

* fix class name

* tests for models

* correct formatting and comments

* Change meta classes

* Fix code format and imports

* Delete all fruit files

* Delete last fruit migration

* Fix migration issue

* change data fields

* Apply changes after review

* apply initial migrations

* Fix contributor type error

* Add verbose names

* Fix import error

* Apply changes from second review

* Migrate models

---------

Co-authored-by: Stefan Kairinos <[email protected]>
  • Loading branch information
SalmanAsh and SKairinos authored Jul 12, 2024
1 parent 81ac9d1 commit 507407d
Show file tree
Hide file tree
Showing 21 changed files with 332 additions and 216 deletions.
29 changes: 29 additions & 0 deletions api/fixtures/agreement_signatures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"model": "api.agreementsignature",
"pk": 1,
"fields": {
"contributor": 1,
"agreement_id": "g3d3d3s8dgd3vc37232fef32232df3f3f31fgawf",
"signed_at": "2024-01-02T12:00:00Z"
}
},
{
"model": "api.agreementsignature",
"pk": 2,
"fields": {
"contributor": 2,
"agreement_id": "g3d3d3s8dgd43vc37232fef0898df3f3f31fgawf",
"signed_at": "2024-01-02T12:00:00Z"
}
},
{
"model": "api.agreementsignature",
"pk": 3,
"fields": {
"contributor": 3,
"agreement_id": "g379tuehr8dgd43vc37232fef0898df3f3f31fga",
"signed_at": "2024-01-02T12:00:00Z"
}
}
]
35 changes: 35 additions & 0 deletions api/fixtures/contributors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"model": "api.contributor",
"pk": 1,
"fields": {
"email": "[email protected]",
"name": "contributor1",
"location": "Hatfield",
"html_url": "https://github.com/contributor1",
"avatar_url": "https://contributor1.github.io/gravatar-url-generator/#/"
}
},
{
"model": "api.contributor",
"pk": 2,
"fields": {
"email": "[email protected]",
"name": "contributor2",
"location": "Hatfield",
"html_url": "https://github.com/contributor2",
"avatar_url": "https://contributor2.github.io/gravatar-url-generator/#/"
}
},
{
"model": "api.contributor",
"pk": 3,
"fields": {
"email": "[email protected]",
"name": "contributor3",
"location": "Hatfield",
"html_url": "https://github.com/contributor3",
"avatar_url": "https://contributor3.github.io/gravatar-url-generator/#/"
}
}
]
29 changes: 0 additions & 29 deletions api/fixtures/fruits.json

This file was deleted.

30 changes: 30 additions & 0 deletions api/fixtures/repositories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"model": "api.repository",
"pk": 1,
"fields": {
"contributor": 1,
"points": 10,
"gh_id": "10274252"
}
},
{
"model": "api.repository",
"pk": 2,
"fields": {
"contributor": 2,
"points": 20,
"gh_id": "102097552"

}
},
{
"model": "api.repository",
"pk": 3,
"fields": {
"contributor": 3,
"points": 30,
"gh_id": "890732552"
}
}
]
45 changes: 38 additions & 7 deletions api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 3.2.25 on 2024-07-02 15:57
# Generated by Django 3.2.25 on 2024-07-12 15:33

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
Expand All @@ -12,16 +13,46 @@ class Migration(migrations.Migration):

operations = [
migrations.CreateModel(
name='Fruit',
name='Contributor',
fields=[
('id', models.IntegerField(help_text="The contributor's GitHub user-ID.", primary_key=True, serialize=False)),
('email', models.EmailField(max_length=254, verbose_name='email')),
('name', models.TextField(verbose_name='name')),
('location', models.TextField(verbose_name='location')),
('html_url', models.TextField(verbose_name='html url')),
('avatar_url', models.TextField(verbose_name='avatar url')),
],
options={
'verbose_name': 'contributor',
'verbose_name_plural': 'contributors',
},
),
migrations.CreateModel(
name='Repository',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('gh_id', models.IntegerField(help_text='Github ID of the repo a contributor has contributed to.', verbose_name='GitHub ID')),
('points', models.IntegerField(default=0, help_text='Story points the contributor closed for this repository.', verbose_name='points')),
('contributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.contributor')),
],
options={
'verbose_name': 'repository',
'verbose_name_plural': 'repositories',
'unique_together': {('contributor', 'gh_id')},
},
),
migrations.CreateModel(
name='AgreementSignature',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=50, verbose_name='name')),
('is_citrus', models.BooleanField(verbose_name='is citrus')),
('expires_on', models.DateField(verbose_name='expires on')),
('agreement_id', models.CharField(help_text='Commit ID of the contribution agreement in workspace.', max_length=40, verbose_name='agreement id')),
('signed_at', models.DateTimeField(verbose_name='signed at')),
('contributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.contributor')),
],
options={
'verbose_name': 'fruit',
'verbose_name_plural': 'fruits',
'verbose_name': 'agreement signature',
'verbose_name_plural': 'agreement signatures',
'unique_together': {('contributor', 'agreement_id')},
},
),
]
4 changes: 3 additions & 1 deletion api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
Created on 02/07/2024 at 11:57:31(+01:00).
"""

from .fruit import Fruit
from .agreement_signature import AgreementSignature
from .contributor import Contributor
from .repository import Repository
40 changes: 40 additions & 0 deletions api/models/agreement_signature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
© Ocado Group
Created on 08/07/2024 at 10:48:44(+01:00).
"""

import typing as t

from django.db import models
from django.utils.translation import gettext_lazy as _

from .contributor import Contributor

if t.TYPE_CHECKING:
from django_stubs_ext.db.models import TypedModelMeta # pragma: no cover
else:
TypedModelMeta = object


class AgreementSignature(models.Model):
"""Signature of a contributor signing the agreement"""

contributor_id: int
contributor = models.ForeignKey(Contributor, on_delete=models.CASCADE)

agreement_id = models.CharField(
_("agreement id"),
max_length=40,
help_text=_("Commit ID of the contribution agreement in workspace."),
)
signed_at = models.DateTimeField(_("signed at"))

class Meta(TypedModelMeta):
unique_together = ["contributor", "agreement_id"]
verbose_name = _("agreement signature")
verbose_name_plural = _("agreement signatures")

def __str__(self):
cont = f"Contributor {self.contributor.pk} signed"
repo = f"{self.agreement_id[:7]} at {self.signed_at}"
return f"{cont} {repo}"
31 changes: 31 additions & 0 deletions api/models/agreement_signature_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
© Ocado Group
Created on 09/07/2024 at 11:43:42(+01:00).
"""

from codeforlife.tests import ModelTestCase

from .agreement_signature import AgreementSignature


class TestAgreementSignature(ModelTestCase[AgreementSignature]):
"""Test the AgreementSignature Model"""

fixtures = ["contributors", "agreement_signatures"]

def setUp(self):
self.agreement_signature = AgreementSignature.objects.get(pk=1)

def test_str(self):
"""
Parsing an agreement-signature instance to a string
that returns the contributor's primary key,
the first 7 characters of the agreement's commit ID
and the timestamp of when the agreement was signed.
"""
commit_id = self.agreement_signature.agreement_id[:7]
time = self.agreement_signature.signed_at
cont = f"Contributor {self.agreement_signature.contributor.pk} signed"
repo = f"{commit_id} at {time}"
expected_str = f"{cont} {repo}"
assert str(self.agreement_signature) == expected_str
34 changes: 34 additions & 0 deletions api/models/contributor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
© Ocado Group
Created on 05/07/2024 at 16:18:48(+01:00).
"""

import typing as t

from django.db import models
from django.utils.translation import gettext_lazy as _

if t.TYPE_CHECKING:
from django_stubs_ext.db.models import TypedModelMeta # pragma: no cover
else:
TypedModelMeta = object


class Contributor(models.Model):
"""A contributor that contributes to a repo"""

id = models.IntegerField(
primary_key=True, help_text=_("The contributor's GitHub user-ID.")
)
email = models.EmailField(_("email"))
name = models.TextField(_("name"))
location = models.TextField(_("location"))
html_url = models.TextField(_("html url"))
avatar_url = models.TextField(_("avatar url"))

class Meta(TypedModelMeta):
verbose_name = _("contributor")
verbose_name_plural = _("contributors")

def __str__(self):
return f"{self.name} <{self.email}>"
25 changes: 25 additions & 0 deletions api/models/contributor_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
© Ocado Group
Created on 09/07/2024 at 09:39:50(+01:00).
"""

from codeforlife.tests import ModelTestCase

from .contributor import Contributor


class TestContributor(ModelTestCase[Contributor]):
"""Test the Contributor Model"""

fixtures = ["contributors"]

def setUp(self):
self.contributor = Contributor.objects.get(pk=1)

def test_str(self):
"""
Parsing a contributor instance to a string returns its name and email.
"""
name = self.contributor.name
email = self.contributor.email
assert str(self.contributor) == f"{name} <{email}>"
32 changes: 0 additions & 32 deletions api/models/fruit.py

This file was deleted.

28 changes: 0 additions & 28 deletions api/models/fruit_test.py

This file was deleted.

Loading

0 comments on commit 507407d

Please sign in to comment.