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

Created SuccessStory model in DB, added model to admin, created seria… #329

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ ignore =
exclude =
__pycache__
testing.py
migrations
24 changes: 12 additions & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Location,
Scholarship,
ScholarshipApplication,
SuccessStory,
TeamMember,
)

Expand Down Expand Up @@ -73,3 +74,13 @@ class CodeSchoolAdmin(admin.ModelAdmin):
)

search_fields = ("name", "rep_name", "rep_email", "url")


@admin.register(SuccessStory)
class SuccessStoryAdmin(admin.ModelAdmin):
list_display = (
"created_by",
"created_at",
"text",
"is_approved",
)
31 changes: 31 additions & 0 deletions src/api/migrations/0009_successstory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 2.2.10 on 2020-05-01 14:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("api", "0008_codeschool_is_vet_tec_approved"),
]

operations = [
migrations.CreateModel(
name="SuccessStory",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created_by", models.CharField(blank=True, max_length=256, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("text", models.TextField(blank=True, null=True)),
("is_approved", models.BooleanField(blank=True, null=True)),
],
),
]
33 changes: 33 additions & 0 deletions src/api/migrations/0010_auto_20200505_1711.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 2.2.10 on 2020-05-05 17:11

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


class Migration(migrations.Migration):

dependencies = [
("api", "0009_successstory"),
]

operations = [
migrations.AlterModelOptions(
name="successstory", options={"verbose_name_plural": "Success Stories"},
),
migrations.AlterField(
model_name="successstory",
name="created_by",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
to=settings.AUTH_USER_MODEL,
),
),
migrations.AlterField(
model_name="successstory",
name="is_approved",
field=models.BooleanField(default=False),
),
]
19 changes: 19 additions & 0 deletions src/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,22 @@ def __str__(self):

class Meta:
db_table = "api_team_members"


class SuccessStory(models.Model):
"""
Model that holds user success stories.
"""

created_by = models.ForeignKey(
User, models.DO_NOTHING, blank=True, null=True, unique=False
)
created_at = models.DateTimeField(auto_now_add=True, unique=False)
text = models.TextField(blank=True, null=True, unique=False)
is_approved = models.BooleanField(default=False, unique=False)

def __str__(self):
return f"{self.created_by} - {self.is_approved}"

class Meta:
verbose_name_plural = "Success Stories"
7 changes: 7 additions & 0 deletions src/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Location,
Scholarship,
ScholarshipApplication,
SuccessStory,
TeamMember,
)

Expand Down Expand Up @@ -39,3 +40,9 @@ class TeamMemberSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = TeamMember
fields = "__all__"


class SuccessStorySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = SuccessStory
fields = "__all__"
1 change: 1 addition & 0 deletions src/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
router.register("scholarships", views.ScholarshipViewSet)
router.register("scholarshipApplications", views.ScholarshipApplicationViewSet)
router.register("teamMembers", views.TeamMemberViewSet)
router.register("sucessStory", views.SuccessStoryViewSet)

urlpatterns = [path("", include(router.urls))]
12 changes: 10 additions & 2 deletions src/api/views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet

from api.models import (
CodeSchool,
Location,
Scholarship,
ScholarshipApplication,
SuccessStory,
TeamMember,
)
from api.serializers import (
CodeSchoolSerializer,
LocationSerializer,
ScholarshipApplicationSerializer,
ScholarshipSerializer,
SuccessStorySerializer,
TeamMemberSerializer,
)

Expand Down Expand Up @@ -41,3 +43,9 @@ class ScholarshipApplicationViewSet(ReadOnlyModelViewSet):
class TeamMemberViewSet(ReadOnlyModelViewSet):
serializer_class = TeamMemberSerializer
queryset = TeamMember.objects.all()


class SuccessStoryViewSet(ModelViewSet):
serializer_class = SuccessStorySerializer
queryset = SuccessStory.objects.all()
permission_classes = (IsAuthenticatedOrReadOnly,)
13 changes: 13 additions & 0 deletions src/tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import threading

from allauth.account.models import EmailAddress
Expand All @@ -13,14 +14,17 @@
SubFactory,
django,
)
from faker import Faker

from api.models import SuccessStory
from core.models import Profile
from tests.test_data import (
DEFAULT_PASSWORD,
fake,
random_branch,
random_mos,
random_pay_grade,
random_text,
)


Expand Down Expand Up @@ -86,3 +90,12 @@ class Meta:
)
profile = RelatedFactory(ProfileFactory, "user")
active_email = RelatedFactory(EmailAddressFactory, "user", email=email)


class SuccessFactory(DjangoModelFactory):
class Meta:
model = SuccessStory

created_at = datetime.datetime.now()
text = LazyFunction(fake.paragraph)
is_approved = LazyFunction(fake.pybool)
8 changes: 8 additions & 0 deletions src/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ def random_profile_dict(request):
profile = request.param
profile.pop("user")
return request.param


@pytest.fixture
def success_story(db, user):
story = f.SuccessFactory()
story.created_by = user
story.save()
return story
14 changes: 14 additions & 0 deletions src/tests/integration/test_success_story.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from django.db import models

from api.models import SuccessStory
from tests.factories import SuccessFactory


@pytest.mark.django_db
def test_stories(success_story):
db = SuccessStory.objects.all()
assert db.get(created_by=success_story.created_by)
assert db.get(created_at=success_story.created_at)
assert db.get(text=success_story.text)
assert db.get(is_approved=success_story.is_approved)
4 changes: 4 additions & 0 deletions src/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def random_branch():
)


def random_text():
return fake.random_text(max_nb_chars=200)


def random_pay_grade():
return f"{fake.random_element(('E', 'O'))}{fake.random_digit()}"

Expand Down