Skip to content

Commit

Permalink
feat(Django): migrate models (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Jul 23, 2024
1 parent f984162 commit 65a1693
Show file tree
Hide file tree
Showing 21 changed files with 329 additions and 1 deletion.
9 changes: 8 additions & 1 deletion config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@
"django_extensions", # django-extensions
]

LOCAL_APPS = ["open_prices.www"]
LOCAL_APPS = [
"open_prices.common",
"open_prices.products",
"open_prices.locations",
"open_prices.proofs",
"open_prices.prices",
"open_prices.www",
]

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

Expand Down
Empty file added open_prices/__init__.py
Empty file.
Empty file added open_prices/common/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions open_prices/common/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from babel.numbers import list_currencies

CURRENCY_LIST = [currency.upper() for currency in list_currencies()]
CURRENCY_CHOICES = [(key, key) for key in CURRENCY_LIST]
Empty file.
18 changes: 18 additions & 0 deletions open_prices/locations/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.contrib import admin

from open_prices.locations.models import Location


@admin.register(Location)
class LocationAdmin(admin.ModelAdmin):
list_display = (
"osm_id",
"osm_type",
"osm_name",
"osm_tag_key",
"osm_tag_value",
"price_count",
"created",
)
list_filter = ("osm_type",)
search_fields = ("osm_id",)
6 changes: 6 additions & 0 deletions open_prices/locations/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
OSM_TYPE_NODE = "NODE"
OSM_TYPE_WAY = "WAY"
OSM_TYPE_RELATION = "RELATION"

OSM_TYPE_LIST = [OSM_TYPE_NODE, OSM_TYPE_WAY, OSM_TYPE_RELATION]
OSM_TYPE_CHOICES = ((key, key) for key in OSM_TYPE_LIST)
37 changes: 37 additions & 0 deletions open_prices/locations/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.db import models
from django.utils import timezone

from open_prices.locations import constants as location_constants


class Location(models.Model):
osm_id = models.PositiveBigIntegerField(blank=True, null=True)
osm_type = models.CharField(
max_length=10, choices=location_constants.OSM_TYPE_CHOICES
)

osm_name = models.CharField(blank=True, null=True)
osm_display_name = models.CharField(blank=True, null=True)
osm_tag_key = models.CharField(blank=True, null=True)
osm_tag_value = models.CharField(blank=True, null=True)
osm_address_postcode = models.CharField(blank=True, null=True)
osm_address_city = models.CharField(blank=True, null=True)
osm_address_country = models.CharField(blank=True, null=True)
osm_address_country_code = models.CharField(blank=True, null=True)
osm_lat = models.DecimalField(
max_digits=11, decimal_places=7, blank=True, null=True
)
osm_lon = models.DecimalField(
max_digits=11, decimal_places=7, blank=True, null=True
)

price_count = models.PositiveIntegerField(blank=True, null=True)

created = models.DateTimeField(default=timezone.now)
updated = models.DateTimeField(auto_now=True)

class Meta:
managed = False
db_table = "locations"
verbose_name = "Location"
verbose_name_plural = "Locations"
Empty file added open_prices/prices/admin.py
Empty file.
9 changes: 9 additions & 0 deletions open_prices/prices/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""For raw products (fruits, vegetables, etc.), the price is either
per unit or per kilogram. This enum is used to store this information.
"""

PRICE_PER_UNIT = "UNIT"
PRICE_PER_KILOGRAM = "KILOGRAM"

PRICE_PER_LIST = [PRICE_PER_UNIT, PRICE_PER_KILOGRAM]
PRICE_PER_CHOICES = [(key, key) for key in PRICE_PER_LIST]
73 changes: 73 additions & 0 deletions open_prices/prices/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from django.db import models
from django.utils import timezone

from open_prices.common import constants
from open_prices.locations import constants as location_constants
from open_prices.prices import constants as price_constants


class Price(models.Model):
product_code = models.CharField(blank=True, null=True)
category_tag = models.CharField(blank=True, null=True)
labels_tags = models.JSONField(blank=True, null=True)
product_name = models.CharField(blank=True, null=True)
origins_tags = models.JSONField(blank=True, null=True)
product = models.ForeignKey(
"products.Product",
on_delete=models.SET_NULL,
blank=True,
null=True,
related_name="prices",
)

price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
price_is_discounted = models.BooleanField(blank=True, null=True)
price_without_discount = models.DecimalField(
max_digits=10, decimal_places=2, blank=True, null=True
)
price_per = models.CharField(
max_length=10,
constants=price_constants.PRICE_PER_CHOICES,
blank=True,
null=True,
)
currency = models.CharField(
max_length=3, choices=constants.CURRENCY_CHOICES, blank=True, null=True
)

location_osm_id = models.PositiveBigIntegerField(blank=True, null=True)
location_osm_type = models.CharField(
max_length=10,
choices=location_constants.OSM_TYPE_CHOICES,
blank=True,
null=True,
)
location = models.ForeignKey(
"locations.Location",
on_delete=models.SET_NULL,
blank=True,
null=True,
related_name="prices",
)

date = models.DateField(blank=True, null=True)

proof = models.ForeignKey(
"proofs.Proof",
on_delete=models.SET_NULL,
blank=True,
null=True,
related_name="prices",
)

owner = models.CharField(blank=True, null=True)
source = models.CharField(blank=True, null=True)

created = models.DateTimeField(default=timezone.now)
updated = models.DateTimeField(auto_now=True)

class Meta:
managed = False
db_table = "prices"
verbose_name = "Price"
verbose_name_plural = "Prices"
Empty file.
16 changes: 16 additions & 0 deletions open_prices/products/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.contrib import admin

from open_prices.products.models import Product


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
list_display = (
"code",
"source",
"product_name",
"price_count",
"created",
)
list_filter = ("source",)
search_fields = ("code",)
4 changes: 4 additions & 0 deletions open_prices/products/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from openfoodfacts import Flavor

SOURCE_LIST = [Flavor.off, Flavor.obf, Flavor.opff, Flavor.opf, Flavor.off_pro]
SOURCE_CHOICES = ((key, key) for key in SOURCE_LIST)
37 changes: 37 additions & 0 deletions open_prices/products/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.utils import timezone

from open_prices.products import constants as product_constants


class Product(models.Model):
code = models.CharField(unique=True, blank=True, null=True)

source = models.CharField(max_length=10, choices=product_constants.SOURCE_CHOICES)
source_last_synced = models.DateTimeField(blank=True, null=True)

product_name = models.CharField(blank=True, null=True)
image_url = models.CharField(blank=True, null=True)
product_quantity = models.IntegerField(blank=True, null=True)
product_quantity_unit = models.CharField(blank=True, null=True)
categories_tags = ArrayField(models.CharField(), blank=True)
brands = models.CharField(blank=True, null=True)
brands_tags = ArrayField(models.CharField(), blank=True)
labels_tags = ArrayField(models.CharField(), blank=True)

nutriscore_grade = models.CharField(blank=True, null=True)
ecoscore_grade = models.CharField(blank=True, null=True)
nova_group = models.PositiveIntegerField(blank=True, null=True)
unique_scans_n = models.PositiveIntegerField(blank=True, null=True)

price_count = models.PositiveIntegerField(blank=True, null=True)

created = models.DateTimeField(default=timezone.now)
updated = models.DateTimeField(auto_now=True)

class Meta:
managed = False
db_table = "products"
verbose_name = "Product"
verbose_name_plural = "Products"
17 changes: 17 additions & 0 deletions open_prices/proofs/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.contrib import admin

from open_prices.proofs.models import Proof


@admin.register(Proof)
class ProofAdmin(admin.ModelAdmin):
list_display = (
"type",
"location",
"date",
"currency",
"price_count",
"owner",
"created",
)
list_filter = ("type",)
6 changes: 6 additions & 0 deletions open_prices/proofs/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PRICE_TAG = "PRICE_TAG"
RECEIPT = "RECEIPT"
GDPR_REQUEST = "GDPR_REQUEST"

TYPE_LIST = [PRICE_TAG, RECEIPT, GDPR_REQUEST]
TYPE_CHOICES = [(key, key) for key in TYPE_LIST]
45 changes: 45 additions & 0 deletions open_prices/proofs/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.db import models
from django.utils import timezone

from open_prices.common import constants
from open_prices.locations import constants as location_constants
from open_prices.proofs import constants as proof_constants


class Proof(models.Model):
file_path = models.CharField()
mimetype = models.CharField(blank=True, null=True)
type = models.CharField(max_length=20, choices=proof_constants.TYPE_CHOICES)

location_osm_id = models.PositiveBigIntegerField(blank=True, null=True)
location_osm_type = models.CharField(
max_length=10,
choices=location_constants.OSM_TYPE_CHOICES,
blank=True,
null=True,
)
location = models.ForeignKey(
"locations.Location",
on_delete=models.SET_NULL,
blank=True,
null=True,
related_name="proofs",
)
date = models.DateField(blank=True, null=True)
currency = models.CharField(
max_length=3, choices=constants.CURRENCY_CHOICES, blank=True, null=True
)

price_count = models.PositiveIntegerField(blank=True, null=True)

owner = models.CharField(blank=True, null=True)
source = models.CharField(blank=True, null=True)

created = models.DateTimeField(default=timezone.now)
updated = models.DateTimeField(auto_now=True)

class Meta:
managed = False
db_table = "proofs"
verbose_name = "Proof"
verbose_name_plural = "Proofs"
Empty file added open_prices/users/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions open_prices/users/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.contrib import admin

from open_prices.users.models import User, Session


@admin.register(User)
class UserAdmin(admin.ModelAdmin):
list_display = ("user_id", "is_moderator", "price_count", "created")
list_filter = ("is_moderator",)
search_fields = ("user_id",)


@admin.register(Session)
class SessionAdmin(admin.ModelAdmin):
list_display = ("user", "token", "created", "last_used")
search_fields = ("token",)
33 changes: 33 additions & 0 deletions open_prices/users/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.db import models
from django.utils import timezone


class User(models.Model):
user_id = models.CharField(primary_key=True)

is_moderator = models.BooleanField(default=False)

price_count = models.PositiveIntegerField(blank=True, null=True)

created = models.DateTimeField(default=timezone.now)
# updated = models.DateTimeField(auto_now=True)

class Meta:
managed = False
db_table = "users"
verbose_name = "User"
verbose_name_plural = "Users"


class Session(models.Model):
user = models.ForeignKey("users.User", on_delete=models.DO_NOTHING, related_name="sessions")
token = models.CharField(unique=True)

created = models.DateTimeField(default=timezone.now)
last_used = models.DateTimeField(blank=True, null=True)

class Meta:
managed = False
db_table = "sessions"
verbose_name = "Session"
verbose_name_plural = "Sessions"

0 comments on commit 65a1693

Please sign in to comment.