Skip to content

Commit

Permalink
MES-57: Add HG synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoMoreta committed May 30, 2024
1 parent 0f68bb2 commit 893b762
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 2 deletions.
11 changes: 11 additions & 0 deletions api/client/Assets Links.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
meta {
name: Assets Links
type: http
seq: 1
}

get {
url: {{URL}}/.well-known/assetlinks.json
body: none
auth: none
}
23 changes: 23 additions & 0 deletions api/client/HGSync/Fetch Account.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
meta {
name: Fetch Account
type: http
seq: 1
}

get {
url: {{URL}}/api/v1/fetch/
body: json
auth: none
}

headers {
Authorization: Token {{token}}
}

body:json {
{
"uuid": "",
"cif": "" ,
"email": ""
}
}
33 changes: 33 additions & 0 deletions api/client/HGSync/Preregister Consumer.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
meta {
name: Preregister Consumer
type: http
seq: 3
}

post {
url: {{URL}}/api/v1/preregister/
body: json
auth: none
}

headers {
Authorization: Token {{token}}
}

body:json {
{
"person": {
"cif": "1234567A",
"member_id": "1234",
"name": "Prueba",
"surname": "Consumidora",
"email": "",
"contact_phone": "123123456",
"address": "Calle falsa 123",
"city": "MAD"
},
"email": ""


}
}
34 changes: 34 additions & 0 deletions api/client/HGSync/Preregister Intercoop.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
meta {
name: Preregister Intercoop
type: http
seq: 4
}

post {
url: {{URL}}/api/v1/preregister/
body: json
auth: none
}

headers {
Authorization: Token {{token}}
}

body:json {
{
"person": {
"cif": "1234567A",
"member_id": "1234",
"name": "Prueba",
"surname": "Intercoop",
"email": "",
"contact_phone": "123123456",
"address": "Calle falsa 123",
"city": "MAD",
"is_intercoop": true
},
"email": ""


}
}
44 changes: 44 additions & 0 deletions api/client/HGSync/Preregister Provider.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
meta {
name: Preregister Provider
type: http
seq: 2
}

post {
url: {{URL}}/api/v1/preregister/
body: json
auth: none
}

headers {
Authorization: Token {{token}}
}

body:json {
{
"entity": {
"cif": "1234567A",
"name": "Entidad falsa",
"member_id": "1234",
"description": "Entidad falsa descripción larga",
"short_description": "Entidad falsa descripción corta",
"email": "",
"contact_phone": "123123456",
"address": "Calle falsa 123",
"city" : "MAD",
"latitude": 0,
"longitude": 0,
"legal_form": "cooperativa",
"num_workers": 1,
"telegram_link": "www.telegram.com",
"twitter_link": "www.twitter.com",
"webpage_link": "app.mercadosocial.net",
"facebook_link": "https://www.facebook.com",
"instagram_link": "https://www.instagram.com",
"categories": ["97924fef-a60a-4c9e-adc1-0ce35e0dd8b9", "83d97ae0-538e-43d5-908e-d30b77c4405a"]
},
"email": ""


}
}
10 changes: 10 additions & 0 deletions api/serializers/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework import serializers

from authentication.models import User


class UserSerializer(serializers.ModelSerializer):

class Meta:
model = User
exclude = ["id", "password", "is_staff", "is_superuser"]
4 changes: 4 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from api.views.news import NewsViewSet
from api.views.offers import OffersViewSet
from api.views.provider import EntitiesView
from api.views.user import FetchUserView, PreRegisterUserView

router = DefaultRouter()
router.register("devices", FCMDeviceAuthorizedViewSet)
Expand All @@ -29,6 +30,9 @@
re_path(r"v2/nodes/(?P<node>\d+)/consumer/", ConsumerViewSet.as_view()),

path("v1/entities/", EntitiesView.as_view()),
path("v1/fetch/", FetchUserView.as_view()),
path("v1/preregister/", PreRegisterUserView.as_view())

]


141 changes: 141 additions & 0 deletions api/views/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@

from django.core.exceptions import MultipleObjectsReturned
from django.http import HttpResponse, HttpResponseNotFound, JsonResponse, HttpResponseForbidden
from rest_framework import authentication, status
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from api.serializers.consumer import ConsumerSerializer
from api.serializers.provider import ProviderSerializer
from api.serializers.user import UserSerializer
from authentication.models import User
from authentication.models.api_token import APIToken
from authentication.models.preregister import PreRegisteredUser
from market.management.commands.import_madrid_data import set_social_profiles, set_categories
from market.models import Account, Provider, Consumer, Category
from django.utils.translation import gettext_lazy as _


class FetchUserView(APIView):
authentication_classes = [authentication.TokenAuthentication]
permission_classes = (IsAuthenticated, IsAdminUser)

def get(self, request, format=None):


data = request.data

instance = None
if 'uuid' in data:
instance = Account.objects.get(id=data['uuid'])
if instance is None and 'cif' in data and data['cif'] is not None:
try:
instance = Account.objects.get(cif=data['cif'])
except MultipleObjectsReturned:
raise Exception(f"Duplicated person. CIF: {data['cif']}. ")
if instance is None and 'email' in data and data['email'] is not None:
try:
instance = Account.objects.get(email=data['email'])
except MultipleObjectsReturned:
raise Exception(f"Duplicated person. Email: {data['email']}")
if instance is None:
return HttpResponseNotFound()

if instance.node != request.user.node:
return HttpResponseForbidden()

response = {"api_key": APIToken.objects.get_or_create(user=instance.owner)[0].key}
if isinstance(instance, Consumer):
response['type'] = "person"
response['person'] = ConsumerSerializer(instance).data
if (response['person'] is not None) and 'owner' in response['person']:
response['person']['id'] = instance.id
del response["person"]["owner"]

response['person']['profile_image'] = instance.profile_image.url if instance.profile_image else None

if response['person']['favorites']:
for i, uuid in enumerate(response['person']['favorites']):
response['person']['favorites'][i] = uuid
elif isinstance(instance, Provider):
response['type'] = "entity"
response['entity'] = ProviderSerializer(instance).data
if (response['entity'] is not None) and 'user' in response['entity']:
response['entity']['id'] = instance.id
if response['entity']['logo']:
response['entity']['logo'] = instance.profile_image.url if instance.profile_image else None
del response['entity']['user']
del response['entity']['gallery']

if instance.categories.count() > 0:
response['entity']['categories'] = []
for cat in instance.categories.all():
response['entity']['categories'].append(cat.pk)

response['user'] = UserSerializer(instance.owner).data
response['user']['is_registered'] = instance.owner.is_registered()

return Response(data=response, status=status.HTTP_200_OK)


class PreRegisterUserView(APIView):
authentication_classes = [authentication.TokenAuthentication]
permission_classes = (IsAuthenticated, IsAdminUser)

def post(self, request, *args, **kwargs):
data = request.data

account = None
email = data['email']

if not request.user.node:
return HttpResponse(_("Este usuario no gestiona ningún mercado."), status=400)

if User.objects.filter(email=email).exists():
return HttpResponse(_("Ya existe un usuario registrado con este correo electrónico."), status=400)

result = {}
if 'person' in data:
consumer = data['person']
account = Consumer.objects.create(
node=request.user.node,
cif=consumer.get('cif'),
email=email,
member_id=consumer.get('member_id'),
first_name=consumer.get('name'),
last_name=consumer.get('surname'),
address=consumer.get('address'),
city=consumer.get('city'),
phone_number=consumer.get('contact_phone'),
is_intercoop=consumer.get('is_intercoop', False)
)
result = {"person": {"id": account.id}}
elif 'entity' in data:
provider = data['entity']
account = Provider.objects.create(
node=request.user.node,
cif=provider.get('cif'),
email=email,
member_id=provider.get('member_id'),
name=provider.get('name'),
address=provider.get('address'),
city=provider.get('city'),
description=provider.get('description'),
short_description=provider.get('short_description'),
latitude=provider.get('latitude', 0),
longitude=provider.get('longitude', 0),
num_workers=provider.get('num_workers', 1),
legal_form=provider.get('legal_form'),
webpage_link=provider.get('webpage_link'),
phone_number=provider.get('contact_phone'),
)
set_social_profiles(provider, account)
set_categories(provider, account)

result = {"entity": {"id": account.id}}
else:
return HttpResponse(_("No existe objeto entity o person"), status=400)

PreRegisteredUser.create_user_and_preregister(account)
return JsonResponse(result, status=200)
3 changes: 3 additions & 0 deletions authentication/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def __str__(self):
def is_preregistered(self):
return self.preregister.all().count() > 0

def is_registered(self):
return self.preregister.count() == 0

@property
def display_name(self):
return self.first_name or self.email
8 changes: 6 additions & 2 deletions market/management/commands/import_madrid_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def set_social_profiles(provider_data, provider_instance):
ProviderSocialProfile.objects.create(social_network=social_network, url=url, provider=provider_instance)


def set_categories(provider_data, provider_instance):
if 'categories' in provider_data and provider_data['categories']:
provider_instance.categories.set(Category.objects.filter(pk__in=provider_data['categories']))


def set_offers(provider_data, provider_instance):

for offer in provider_data['offers']:
Expand Down Expand Up @@ -260,8 +265,7 @@ def import_providers(providers, node):

provider_created.profile_image.name = item['logo']

if item['categories']:
provider_created.categories.set(Category.objects.filter(id__in=item['categories']))
set_categories(item, provider_created)

provider_created.save()

Expand Down

0 comments on commit 893b762

Please sign in to comment.