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

[Tech] Refactoring des constants #984

Merged
merged 6 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lemarche/api/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from lemarche.perimeters.factories import PerimeterFactory
from lemarche.sectors.factories import SectorFactory
from lemarche.tenders import constants as tender_constants
from lemarche.tenders.models import Tender
from lemarche.users.factories import UserFactory
from lemarche.users.models import User
Expand Down Expand Up @@ -65,8 +66,8 @@ def test_user_with_valid_api_key_can_create_tender(self):
tender = Tender.objects.get(title="Test author 1")
self.assertEqual(User.objects.count(), 2 + 1) # created a new user
self.assertEqual(tender.author.email, USER_CONTACT_EMAIL)
self.assertEqual(tender.status, Tender.STATUS_PUBLISHED)
self.assertEqual(tender.source, Tender.SOURCE_API)
self.assertEqual(tender.status, tender_constants.STATUS_PUBLISHED)
self.assertEqual(tender.source, tender_constants.SOURCE_API)
self.assertNotEqual(tender.import_raw_object, None)
self.assertEqual(tender.import_raw_object["title"], "Test author 1")
# test with own email
Expand All @@ -79,8 +80,8 @@ def test_user_with_valid_api_key_can_create_tender(self):
tender = Tender.objects.get(title="Test author 2")
self.assertEqual(User.objects.count(), 3) # did not create a new user
self.assertEqual(tender.author, self.user_with_token)
self.assertEqual(tender.status, Tender.STATUS_PUBLISHED)
self.assertEqual(tender.source, Tender.SOURCE_API)
self.assertEqual(tender.status, tender_constants.STATUS_PUBLISHED)
self.assertEqual(tender.source, tender_constants.SOURCE_API)
self.assertNotEqual(tender.import_raw_object, None)
self.assertEqual(tender.import_raw_object["title"], "Test author 2")

Expand Down Expand Up @@ -141,7 +142,7 @@ def test_create_tender_with_tally_source(self):
response = self.client.post(url, data=tender_data, content_type="application/json")
self.assertEqual(response.status_code, 201)
tender = Tender.objects.get(title="Test tally")
self.assertEqual(tender.source, Tender.SOURCE_TALLY)
self.assertEqual(tender.source, tender_constants.SOURCE_TALLY)


class TenderChoicesApiTest(TestCase):
Expand Down
14 changes: 8 additions & 6 deletions lemarche/api/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from lemarche.api.utils import BasicChoiceSerializer, check_user_token
from lemarche.tenders import constants as tender_constants
from lemarche.tenders.models import Tender
from lemarche.users.models import User
from lemarche.users import constants as user_constants
from lemarche.www.tenders.utils import get_or_create_user_from_anonymous_content


Expand All @@ -27,17 +27,19 @@ def create(self, request, *args, **kwargs):

def perform_create(self, serializer: TenderSerializer):
source = (
Tender.SOURCE_TALLY
if serializer.validated_data.get("extra_data", {}).get("source") == Tender.SOURCE_TALLY
else Tender.SOURCE_API
tender_constants.SOURCE_TALLY
if serializer.validated_data.get("extra_data", {}).get("source") == tender_constants.SOURCE_TALLY
else tender_constants.SOURCE_API
)
user = get_or_create_user_from_anonymous_content(
serializer.validated_data,
source=User.SOURCE_TALLY_FORM if source == Tender.SOURCE_TALLY else User.SOURCE_SIGNUP_FORM,
source=user_constants.SOURCE_TALLY_FORM
if source == tender_constants.SOURCE_TALLY
else user_constants.SOURCE_SIGNUP_FORM,
)
serializer.save(
author=user,
status=Tender.STATUS_PUBLISHED,
status=tender_constants.STATUS_PUBLISHED,
published_at=timezone.now(),
source=source,
import_raw_object=self.request.data,
Expand Down
18 changes: 9 additions & 9 deletions lemarche/cocorico/management/commands/migrate_data_to_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@
"company_addr_string",
]

DIRECTORY_BOOLEAN_FIELDS = [field.name for field in Siae._meta.fields if type(field) == BooleanField]
USER_BOOLEAN_FIELDS = [field.name for field in User._meta.fields if type(field) == BooleanField]
DIRECTORY_BOOLEAN_FIELDS = [field.name for field in Siae._meta.fields if type(field) is BooleanField]
USER_BOOLEAN_FIELDS = [field.name for field in User._meta.fields if type(field) is BooleanField]

DIRECTORY_DATE_FIELDS = [field.name for field in Siae._meta.fields if type(field) == DateTimeField]
NETWORK_DATE_FIELDS = [field.name for field in Network._meta.fields if type(field) == DateTimeField]
SECTOR_DATE_FIELDS = [field.name for field in Sector._meta.fields if type(field) == DateTimeField]
USER_DATE_FIELDS = [field.name for field in User._meta.fields if type(field) == DateTimeField]
DIRECTORY_DATE_FIELDS = [field.name for field in Siae._meta.fields if type(field) is DateTimeField]
NETWORK_DATE_FIELDS = [field.name for field in Network._meta.fields if type(field) is DateTimeField]
SECTOR_DATE_FIELDS = [field.name for field in Sector._meta.fields if type(field) is DateTimeField]
USER_DATE_FIELDS = [field.name for field in User._meta.fields if type(field) is DateTimeField]


def integer_to_boolean(elem):
Expand Down Expand Up @@ -101,8 +101,8 @@ def make_aware_dates(elem):
def map_siae_nature(input_value):
if input_value:
nature_mapping = {
"siege": Siae.NATURE_HEAD_OFFICE,
"antenne": Siae.NATURE_ANTENNA,
"siege": siae_constants.NATURE_HEAD_OFFICE,
"antenne": siae_constants.NATURE_ANTENNA,
"n/a": None,
None: None,
}
Expand Down Expand Up @@ -625,7 +625,7 @@ def migrate_siae_image(self, cur): # noqa C901
# print("images missing", siae_listing)
error_count["listing_without_image"] += 1
else:
for (index, siae_image) in enumerate(siae_listing["images"]):
for index, siae_image in enumerate(siae_listing["images"]):
siae_image_dict = siae_listing.copy() | siae_image

# rename fields
Expand Down
6 changes: 3 additions & 3 deletions lemarche/notes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from lemarche.notes.models import Note
from lemarche.siaes.factories import SiaeFactory
from lemarche.tenders.factories import TenderFactory
from lemarche.users import constants as user_constants
from lemarche.users.factories import UserFactory
from lemarche.utils import constants


class NoteModelTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = UserFactory(kind=constants.USER_KIND_ADMIN)
cls.user = UserFactory(kind=user_constants.KIND_ADMIN)
cls.note = NoteFactory()
cls.note_with_author = NoteFactory(author=cls.user)

Expand Down Expand Up @@ -41,7 +41,7 @@ def test_create_siae_note_with_generic_relation(self):
self.assertEqual(siae.notes.count(), 2)

def test_create_user_note_with_generic_relation(self):
user_siae = UserFactory(kind=constants.USER_KIND_SIAE)
user_siae = UserFactory(kind=user_constants.KIND_SIAE)
NoteFactory(author=self.user, content_object=user_siae)
self.assertEqual(Note.objects.count(), 2 + 1)
# can create multiple notes for the same object
Expand Down
5 changes: 3 additions & 2 deletions lemarche/siaes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from lemarche.conversations.models import Conversation
from lemarche.labels.models import Label
from lemarche.notes.models import Note
from lemarche.siaes import constants as siae_constants
from lemarche.siaes.models import (
Siae,
SiaeClientReference,
Expand Down Expand Up @@ -410,7 +411,7 @@ def get_readonly_fields(self, request, obj=None):
if not obj:
return add_readonly_fields
# also allow edition of some specific Siae
if obj and obj.source in [Siae.SOURCE_STAFF_C4_CREATED, Siae.SOURCE_ESAT]:
if obj and obj.source in [siae_constants.SOURCE_STAFF_C4_CREATED, siae_constants.SOURCE_ESAT]:
return add_readonly_fields + ["name"]
# for the rest, use the default full-version list
return self.readonly_fields
Expand All @@ -428,7 +429,7 @@ def get_changeform_initial_data(self, request):
"""
Default values in add form.
"""
return {"is_active": False, "source": Siae.SOURCE_STAFF_C4_CREATED}
return {"is_active": False, "source": siae_constants.SOURCE_STAFF_C4_CREATED}

def lookup_allowed(self, lookup, *args, **kwargs):
if lookup in [
Expand Down
28 changes: 28 additions & 0 deletions lemarche/siaes/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,34 @@
(GEO_RANGE_CUSTOM, "Distance en kilomètres"),
)

SOURCE_ASP = "ASP"
SOURCE_GEIQ = "GEIQ"
SOURCE_EA_EATT = "EA_EATT"
SOURCE_USER_CREATED = "USER_CREATED"
SOURCE_STAFF_C1_CREATED = "STAFF_C1_CREATED"
SOURCE_STAFF_C4_CREATED = "STAFF_C4_CREATED"
SOURCE_ESAT = "ESAT"
SOURCE_SEP = "SEP"

SOURCE_CHOICES = (
(SOURCE_ASP, "Export ASP"),
(SOURCE_GEIQ, "Export GEIQ"),
(SOURCE_EA_EATT, "Export EA+EATT"),
(SOURCE_USER_CREATED, "Utilisateur (Antenne)"),
(SOURCE_STAFF_C1_CREATED, "Staff C1"),
(SOURCE_STAFF_C4_CREATED, "Staff C4"),
(SOURCE_ESAT, "Import ESAT (GSAT, Handeco)"),
(SOURCE_SEP, "Import SEP"),
)

NATURE_HEAD_OFFICE = "HEAD_OFFICE"
NATURE_ANTENNA = "ANTENNA"

NATURE_CHOICES = (
(NATURE_HEAD_OFFICE, "Conventionné par la DREETS"),
(NATURE_ANTENNA, "Rattaché à un autre conventionnement"),
)


COMPLETION_KIND_KEY = "kind"
COMPLETION_SCORE_KEY = "score"
Expand Down
2 changes: 1 addition & 1 deletion lemarche/siaes/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Meta:
name = factory.Faker("company", locale="fr_FR")
# slug auto-generated
kind = siae_constants.KIND_EI
nature = factory.fuzzy.FuzzyChoice([key for (key, value) in Siae.NATURE_CHOICES])
nature = factory.fuzzy.FuzzyChoice([key for (key, value) in siae_constants.NATURE_CHOICES])
presta_type = factory.List([factory.fuzzy.FuzzyChoice([key for (key, value) in siae_constants.PRESTA_CHOICES])])
# Don't start a SIRET with 0.
siret = factory.fuzzy.FuzzyText(length=13, chars=string.digits, prefix="1")
Expand Down
2 changes: 1 addition & 1 deletion lemarche/siaes/management/commands/import_esat_from_asp.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def import_esat(self, esat): # noqa C901

# defaults
esat["kind"] = siae_constants.KIND_ESAT
esat["source"] = Siae.SOURCE_ESAT
esat["source"] = siae_constants.SOURCE_ESAT
esat["geo_range"] = siae_constants.GEO_RANGE_DEPARTMENT

# basic fields
Expand Down
6 changes: 4 additions & 2 deletions lemarche/siaes/management/commands/import_esat_gesat.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def import_esat(self, esat): # noqa C901

# defaults
esat["kind"] = siae_constants.KIND_ESAT
esat["source"] = Siae.SOURCE_ESAT
esat["source"] = siae_constants.SOURCE_ESAT
esat["geo_range"] = siae_constants.GEO_RANGE_DEPARTMENT

# basic fields
Expand Down Expand Up @@ -194,7 +194,9 @@ def import_esat(self, esat): # noqa C901
esat["siret"], reason="Mise à jour donnéés Marché de la plateforme de l'Inclusion"
)
if etablissement:
esat["nature"] = Siae.NATURE_HEAD_OFFICE if etablissement["is_head_office"] else Siae.NATURE_ANTENNA
esat["nature"] = (
siae_constants.NATURE_HEAD_OFFICE if etablissement["is_head_office"] else siae_constants.NATURE_ANTENNA
)
# esat["is_active"] = False if not etablissement["is_closed"] else True
esat["api_entreprise_employees"] = etablissement["employees"]
if etablissement["date_constitution"]:
Expand Down
6 changes: 4 additions & 2 deletions lemarche/siaes/management/commands/import_esat_handeco.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def import_esat(self, esat): # noqa C901

# defaults
esat["kind"] = siae_constants.KIND_ESAT
esat["source"] = Siae.SOURCE_ESAT
esat["source"] = siae_constants.SOURCE_ESAT
esat["geo_range"] = siae_constants.GEO_RANGE_DEPARTMENT

# basic fields
Expand Down Expand Up @@ -166,7 +166,9 @@ def import_esat(self, esat): # noqa C901
esat["siret"], reason="Mise à jour donnéés Marché de la plateforme de l'Inclusion"
)
if etablissement:
esat["nature"] = Siae.NATURE_HEAD_OFFICE if etablissement["is_head_office"] else Siae.NATURE_ANTENNA
esat["nature"] = (
siae_constants.NATURE_HEAD_OFFICE if etablissement["is_head_office"] else siae_constants.NATURE_ANTENNA
)
# esat["is_active"] = False if not etablissement["is_closed"] else True
esat["api_entreprise_employees"] = etablissement["employees"]
if etablissement["date_constitution"]:
Expand Down
4 changes: 2 additions & 2 deletions lemarche/siaes/management/commands/sync_c1_c4.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ def map_siae_presta_type(siae_kind):
def map_siae_nature(siae_source):
if siae_source:
if siae_source in ["ASP", "GEIQ", "EA_EATT"]:
return Siae.NATURE_HEAD_OFFICE
return siae_constants.NATURE_HEAD_OFFICE
if siae_source == "USER_CREATED":
return Siae.NATURE_ANTENNA
return siae_constants.NATURE_ANTENNA
return ""


Expand Down
36 changes: 6 additions & 30 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,34 +544,6 @@ class Siae(models.Model):
"ca",
]

SOURCE_ASP = "ASP"
SOURCE_GEIQ = "GEIQ"
SOURCE_EA_EATT = "EA_EATT"
SOURCE_USER_CREATED = "USER_CREATED"
SOURCE_STAFF_C1_CREATED = "STAFF_C1_CREATED"
SOURCE_STAFF_C4_CREATED = "STAFF_C4_CREATED"
SOURCE_ESAT = "ESAT"
SOURCE_SEP = "SEP"

SOURCE_CHOICES = (
(SOURCE_ASP, "Export ASP"),
(SOURCE_GEIQ, "Export GEIQ"),
(SOURCE_EA_EATT, "Export EA+EATT"),
(SOURCE_USER_CREATED, "Utilisateur (Antenne)"),
(SOURCE_STAFF_C1_CREATED, "Staff C1"),
(SOURCE_STAFF_C4_CREATED, "Staff C4"),
(SOURCE_ESAT, "Import ESAT (GSAT, Handeco)"),
(SOURCE_SEP, "Import SEP"),
)

NATURE_HEAD_OFFICE = "HEAD_OFFICE"
NATURE_ANTENNA = "ANTENNA"

NATURE_CHOICES = (
(NATURE_HEAD_OFFICE, "Conventionné par la DREETS"),
(NATURE_ANTENNA, "Rattaché à un autre conventionnement"),
)

DEPARTMENT_CHOICES = DEPARTMENTS_PRETTY.items()
REGION_CHOICES = REGIONS_PRETTY.items()

Expand All @@ -589,7 +561,9 @@ class Siae(models.Model):
siret = models.CharField(verbose_name="Siret", validators=[validate_siret], max_length=14, db_index=True)
siret_is_valid = models.BooleanField(verbose_name="Siret Valide", default=False)
naf = models.CharField(verbose_name="Naf", validators=[validate_naf], max_length=5, blank=True)
nature = models.CharField(verbose_name="Établissement", max_length=20, choices=NATURE_CHOICES, blank=True)
nature = models.CharField(
verbose_name="Établissement", max_length=20, choices=siae_constants.NATURE_CHOICES, blank=True
)
presta_type = ChoiceArrayField(
verbose_name="Type de prestation",
base_field=models.CharField(max_length=20, choices=siae_constants.PRESTA_CHOICES),
Expand Down Expand Up @@ -792,7 +766,9 @@ class Siae(models.Model):
"Date de remplissage (basique) de la fiche", blank=True, null=True
)
logs = models.JSONField(verbose_name="Logs historiques", editable=False, default=list)
source = models.CharField(max_length=20, choices=SOURCE_CHOICES, default=SOURCE_STAFF_C4_CREATED)
source = models.CharField(
max_length=20, choices=siae_constants.SOURCE_CHOICES, default=siae_constants.SOURCE_STAFF_C4_CREATED
)
import_raw_object = models.JSONField(verbose_name="Donnée JSON brute", editable=False, null=True)

created_at = models.DateTimeField(verbose_name="Date de création", default=timezone.now)
Expand Down
8 changes: 4 additions & 4 deletions lemarche/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.utils import timezone

from lemarche.siaes import constants as siae_constants
from lemarche.utils import constants
from lemarche.users import constants as user_constants


class TrackerQuerySet(models.QuerySet):
Expand All @@ -22,10 +22,10 @@ def siae_views_last_3_months(self, siae_slug):
)

def siae_buyer_views_last_3_months(self, siae_slug):
return self.env_prod().siae_views_last_3_months(siae_slug).by_user_kind(constants.USER_KIND_BUYER)
return self.env_prod().siae_views_last_3_months(siae_slug).by_user_kind(user_constants.KIND_BUYER)

def siae_partner_views_last_3_months(self, siae_slug):
return self.env_prod().siae_views_last_3_months(siae_slug).by_user_kind(constants.USER_KIND_PARTNER)
return self.env_prod().siae_views_last_3_months(siae_slug).by_user_kind(user_constants.KIND_PARTNER)


class Tracker(models.Model):
Expand All @@ -41,7 +41,7 @@ class Tracker(models.Model):
data = models.JSONField()

user_id = models.IntegerField(blank=True, null=True)
user_kind = models.CharField(max_length=20, choices=constants.USER_KIND_CHOICES_WITH_ADMIN, blank=True)
user_kind = models.CharField(max_length=20, choices=user_constants.KIND_CHOICES_WITH_ADMIN, blank=True)
isadmin = models.BooleanField(default=False) # user.kind == User.KIND_ADMIN

siae_id = models.IntegerField(blank=True, null=True)
Expand Down
8 changes: 4 additions & 4 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from lemarche.notes.models import Note
from lemarche.perimeters.admin import PerimeterRegionFilter
from lemarche.tenders import constants
from lemarche.tenders import constants as tender_constants
from lemarche.tenders.forms import TenderAdminForm
from lemarche.tenders.models import PartnerShareTender, Tender, TenderQuestion, TenderStepsData
from lemarche.utils.admin.admin_site import admin_site
Expand Down Expand Up @@ -68,7 +68,7 @@ class ResponseKindFilter(admin.SimpleListFilter):
parameter_name = "response_kind"

def lookups(self, request, model_admin):
return Tender.RESPONSE_KIND_CHOICES
return tender_constants.RESPONSE_KIND_CHOICES

def queryset(self, request, queryset):
lookup_value = self.value()
Expand Down Expand Up @@ -350,7 +350,7 @@ def get_changeform_initial_data(self, request):
"""
Default values in add form.
"""
return {"source": Tender.SOURCE_STAFF_C4_CREATED}
return {"source": tender_constants.SOURCE_STAFF_C4_CREATED}

def lookup_allowed(self, lookup, *args, **kwargs):
if lookup in [
Expand All @@ -369,7 +369,7 @@ def get_readonly_fields(self, request, obj=None):
if obj.author_id and obj.author_id != request.user.id:
readonly_fields.append("status")
# slug cannot be changed once the tender is validated
if obj.status == constants.STATUS_VALIDATED:
if obj.status == tender_constants.STATUS_VALIDATED:
readonly_fields.append("slug")
return readonly_fields

Expand Down
Loading