Skip to content

Commit

Permalink
Merge pull request #134 from unb-mds/133/refactor/alterar-location-pa…
Browse files Browse the repository at this point in the history
…ra-ser-tratado-como-id-e-incluído-no-barcode

Feat/Refactor(Rodar-Projeto/Item): Automatização de Execução,  Inclusão do campo Location com id único adicionado ao barcode e Atualização do README
  • Loading branch information
Potatoyz908 authored Jan 10, 2025
2 parents f1f5c27 + 2a7ca24 commit 45d2c0a
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __pycache__
__pycache__/
secrets.env
API/media/
start.sh
2 changes: 2 additions & 0 deletions API/chat/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from rest_framework import serializers

from users.models import Item

from .models import ChatRoom, Message


Expand Down
3 changes: 1 addition & 2 deletions API/chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from users.models import Item

from chat.models import ChatRoom, Message
from users.models import Item

from .serializers import ChatRoomSerializer, MessageSerializer

Expand Down
34 changes: 34 additions & 0 deletions API/users/migrations/0005_location_alter_item_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 5.1.4 on 2025-01-10 07:09

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


class Migration(migrations.Migration):

dependencies = [
("users", "0004_merge_20241229_1953"),
]

operations = [
migrations.CreateModel(
name="Location",
fields=[
(
"id",
models.BigAutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("name", models.CharField(max_length=100, unique=True)),
("location_id", models.CharField(max_length=2, unique=True)),
],
),
migrations.AlterField(
model_name="item",
name="location",
field=models.ForeignKey(
null=True, on_delete=django.db.models.deletion.SET_NULL, to="users.location"
),
),
]
15 changes: 13 additions & 2 deletions API/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def __str__(self):
return self.name


class Location(models.Model):
name = models.CharField(max_length=100, unique=True) # Nome do local
location_id = models.CharField(max_length=2, unique=True) # ID único do local

def __str__(self):
return self.name


class Color(models.Model):
name = models.CharField(max_length=50, unique=True) # Nome da cor
color_id = models.CharField(max_length=2, unique=True) # ID único da cor
Expand Down Expand Up @@ -47,7 +55,9 @@ class Item(models.Model):
category = models.ForeignKey(
Category, on_delete=models.SET_NULL, null=True
) # Categoria do item
location = models.CharField(max_length=100) # Local onde foi encontrado ou perdido
location = models.ForeignKey(
Location, on_delete=models.SET_NULL, null=True # Local do item
)
color = models.ForeignKey(
Color, on_delete=models.SET_NULL, null=True, blank=True
) # Cor do item (opcional)
Expand All @@ -65,10 +75,11 @@ class Item(models.Model):
@property
def barcode(self):
category_id = self.category.category_id if self.category else "00"
location_id = self.location.location_id if self.location else "00"
color_id = self.color.color_id if self.color else "00"
brand_id = self.brand.brand_id if self.brand else "00"
is_valuable = "1" if self.is_valuable else "0" # 1 se é valioso e 0 se não
return f"{category_id}{color_id}{brand_id}{is_valuable}"
return f"{category_id}{location_id}{color_id}{brand_id}{is_valuable}"

def __str__(self):
return f"{self.name} ({self.location})"
Expand Down
11 changes: 10 additions & 1 deletion API/users/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import cloudinary.uploader
from rest_framework import serializers

from .models import Brand, Category, Color, Item, ItemImage
from .models import Brand, Category, Color, Item, ItemImage, Location


class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = ["id", "name", "location_id"]


class ColorSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -31,6 +37,9 @@ class ItemSerializer(serializers.ModelSerializer):
)
image_urls = serializers.SerializerMethodField(read_only=True)
barcode = serializers.CharField(read_only=True) # Código único do item
location = serializers.PrimaryKeyRelatedField(
queryset=Location.objects.all(), required=False
)

class Meta:
model = Item
Expand Down
2 changes: 2 additions & 0 deletions API/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
ColorViewSet,
ItemImageViewSet,
ItemViewSet,
LocationViewSet,
TestUserView,
UserDetailView,
)

router = DefaultRouter()
router.register(r"items", ItemViewSet, basename="item")
router.register(r"categories", CategoryViewSet, basename="category")
router.register(r"locations", LocationViewSet, basename="location")
router.register(r"colors", ColorViewSet, basename="color")
router.register(r"brands", BrandViewSet, basename="brand")
router.register(r"items/(?P<item_id>[^/.]+)/images", ItemImageViewSet, basename="item-image")
Expand Down
13 changes: 10 additions & 3 deletions API/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from .models import Brand, Category, Color, Item, ItemImage, UserProfile
from .models import Brand, Category, Color, Item, ItemImage, Location, UserProfile
from .serializers import (
BrandSerializer,
CategorySerializer,
ColorSerializer,
ItemImageSerializer,
ItemSerializer,
LocationSerializer,
)

# Configurações do MSAL
Expand All @@ -43,8 +44,8 @@ class ItemViewSet(ModelViewSet):
serializer_class = ItemSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter]
filterset_fields = ["category", "color", "is_valuable", "status"]
search_fields = ["name", "location", "description"]
filterset_fields = ["category", "location", "color", "is_valuable", "status"]
search_fields = ["name", "description"]
ordering_fields = ["created_at", "found_lost_date"]

def perform_create(self, serializer):
Expand All @@ -57,6 +58,12 @@ class CategoryViewSet(ModelViewSet):
permission_classes = [IsAuthenticatedOrReadOnly]


class LocationViewSet(ModelViewSet):
queryset = Location.objects.all()
serializer_class = LocationSerializer
permission_classes = [IsAuthenticatedOrReadOnly]


class ColorViewSet(ModelViewSet):
queryset = Color.objects.all()
serializer_class = ColorSerializer
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Definir a tarefa 'run' para automatizar o comando
run:
cd API && sudo docker compose up --build

# Adicione outras tarefas, se necessário
pull:
git pull origin main
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ make install

Para iniciar o projeto, use o comando:

```bash
make run
```

ou utilize os seguintes comandos:

```bash
cd API/
```

```bash
docker compose up
```
Expand Down Expand Up @@ -130,7 +140,7 @@ docker exec django-api python3 ./manage.py updatedb -a

| Serviço | URL |
|------------|--------------------------|
| Frontend | http://localhost:3000 |
| Frontend | http://localhost:5173 |
| Backend | http://localhost:8000 |

### 📍 Migrations
Expand Down Expand Up @@ -162,4 +172,4 @@ O protótipo da plataforma está disponível [aqui](https://www.figma.com/proto/

---

AcheiUnB é um software livre, disponível sob a licença MIT.
AcheiUnB é um software livre, disponível sob a licença MIT.

0 comments on commit 45d2c0a

Please sign in to comment.