first commit
This commit is contained in:
commit
3e4574dcdd
163 files changed
+3559
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,10 @@
|
||||
from django.contrib import admin
|
||||
from .models import UserProfile as UserProfileModel
|
||||
|
||||
|
||||
# Register your models here.
|
||||
@admin.register(UserProfileModel)
|
||||
class UserProfileAdmin(admin.ModelAdmin):
|
||||
list_display = ['cuser', 'city',]
|
||||
search_fields = ['cuser__name_family', 'city__name',]
|
||||
list_filter = ['city__is_ostan']
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class UserprofileConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.userprofile'
|
||||
@@ -0,0 +1,33 @@
|
||||
# Generated by Django 4.2.1 on 2024-03-27 14:24
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('appsetting', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='UserProfile',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('codemeli', models.PositiveBigIntegerField()),
|
||||
('address', models.CharField(max_length=500)),
|
||||
('codeposti', models.CharField(max_length=15)),
|
||||
('ax_mojri', models.ImageField(blank=True, null=True, upload_to='mojri/')),
|
||||
('ax_meli', models.ImageField(blank=True, null=True, upload_to='meli/')),
|
||||
('ax_fani', models.ImageField(blank=True, null=True, upload_to='fani/')),
|
||||
('ax_sayer', models.ImageField(blank=True, null=True, upload_to='sayer/')),
|
||||
('city', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='appsetting.city')),
|
||||
('cuser', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='user_profile', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
||||
Whitespace-only changes.
@@ -0,0 +1,21 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import models
|
||||
from apps.appsetting.models import City as CityModel
|
||||
|
||||
# Create your models here.
|
||||
CUserModel = get_user_model()
|
||||
|
||||
|
||||
class UserProfile(models.Model):
|
||||
cuser = models.OneToOneField(CUserModel, on_delete=models.CASCADE, related_name='user_profile')
|
||||
codemeli = models.PositiveBigIntegerField(null=False, blank=False)
|
||||
city = models.ForeignKey(CityModel, on_delete=models.SET_NULL, null=True, blank=True)
|
||||
address = models.CharField(max_length=500, null=False, blank=False)
|
||||
codeposti = models.CharField(max_length=15, null=False, blank=False)
|
||||
ax_mojri = models.ImageField(upload_to='mojri/', null=True, blank=True)
|
||||
ax_meli = models.ImageField(upload_to='meli/', null=True, blank=True)
|
||||
ax_fani = models.ImageField(upload_to='fani/', null=True, blank=True)
|
||||
ax_sayer = models.ImageField(upload_to='sayer/', null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.cuser.name_family}'
|
||||
@@ -0,0 +1,11 @@
|
||||
from apps.userprofile.models import UserProfile as UserProfileModel
|
||||
from rest_framework.validators import ValidationError
|
||||
|
||||
|
||||
def get_user_profile(user_id: int):
|
||||
try:
|
||||
return UserProfileModel.objects.get(cuser_id=user_id)
|
||||
except UserProfileModel.DoesNotExist:
|
||||
raise ValidationError("User profile does not exist")
|
||||
except Exception as exp:
|
||||
raise ValidationError(exp)
|
||||
@@ -0,0 +1,36 @@
|
||||
from apps.userprofile.models import UserProfile as UserProfileModel
|
||||
from rest_framework.validators import ValidationError
|
||||
from apps.cuser.models import CUser as CUserModel
|
||||
|
||||
|
||||
def update_userprofile(user_id: int, **kwargs):
|
||||
try:
|
||||
user_profile_obj = UserProfileModel.objects.get(cuser_id=user_id)
|
||||
|
||||
if 'ax_mojri' in kwargs:
|
||||
user_profile_obj.ax_mojri = kwargs.get('ax_mojri', None)
|
||||
kwargs.pop('ax_mojri')
|
||||
if 'ax_meli' in kwargs:
|
||||
user_profile_obj.ax_meli = kwargs.get('ax_meli', None)
|
||||
kwargs.pop('ax_meli')
|
||||
if 'ax_fani' in kwargs:
|
||||
user_profile_obj.ax_fani = kwargs.get('ax_fani', None)
|
||||
kwargs.pop('ax_fani')
|
||||
if 'ax_sayer' in kwargs:
|
||||
user_profile_obj.ax_sayer = kwargs.get('ax_sayer', None)
|
||||
kwargs.pop('ax_sayer')
|
||||
|
||||
user_profile_obj.save()
|
||||
|
||||
user_profile = UserProfileModel.objects.filter(cuser_id=user_id)
|
||||
name_family = kwargs.get('name_family')
|
||||
if name_family:
|
||||
kwargs.pop('name_family')
|
||||
CUserModel.objects.filter(id=user_id).update(name_family=name_family)
|
||||
|
||||
user_profile.update(**kwargs)
|
||||
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
raise ValidationError(e)
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.urls import path
|
||||
from apps.userprofile.views import UpdateUserProfileView, UserProfileView
|
||||
urlpatterns = [
|
||||
path('', UserProfileView.as_view(), name='profile'),
|
||||
path('update/', UpdateUserProfileView.as_view(), name='update'),
|
||||
]
|
||||
@@ -0,0 +1,114 @@
|
||||
from django.shortcuts import render
|
||||
from rest_framework import serializers
|
||||
from rest_framework.views import APIView
|
||||
from apps.userprofile.models import UserProfile as UserProfileModel
|
||||
from apps.userprofile.services import update_userprofile
|
||||
from apps.userprofile.selector import get_user_profile
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from utils.tools.inline_serializer import inline_serializer
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import User
|
||||
from utils.services.sms_task import task_send_sms
|
||||
|
||||
# Create your views here.
|
||||
|
||||
Cuser = get_user_model()
|
||||
|
||||
class UserProfileView(APIView):
|
||||
permission_classes = (IsAuthenticated,)
|
||||
|
||||
class OutputSerializer(serializers.ModelSerializer):
|
||||
|
||||
cuser = inline_serializer(many=False, fields={
|
||||
"id": serializers.IntegerField(),
|
||||
"username": serializers.CharField(),
|
||||
"name_family": serializers.CharField(),
|
||||
"mobile": serializers.IntegerField(),
|
||||
})
|
||||
|
||||
city = inline_serializer(many=False, fields={
|
||||
"id": serializers.IntegerField(),
|
||||
"name": serializers.CharField(),
|
||||
"is_ostan": serializers.BooleanField(),
|
||||
"ostan": inline_serializer(many=False, fields={
|
||||
"id": serializers.IntegerField(),
|
||||
"name": serializers.CharField(),
|
||||
}),
|
||||
"pv_id": serializers.IntegerField
|
||||
})
|
||||
|
||||
class Meta:
|
||||
model = UserProfileModel
|
||||
fields = "__all__"
|
||||
|
||||
def get(self, request):
|
||||
user_id = request.user.id
|
||||
user_profile = get_user_profile(user_id)
|
||||
serializer = self.OutputSerializer(user_profile)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class UpdateUserProfileView(APIView):
|
||||
permission_classes = (IsAuthenticated, )
|
||||
|
||||
class InputSerializer(serializers.Serializer):
|
||||
name_family = serializers.CharField(required=False)
|
||||
codemeli = serializers.IntegerField(required=False)
|
||||
city = serializers.IntegerField(required=False)
|
||||
address = serializers.CharField(required=False)
|
||||
codeposti = serializers.CharField(required=False)
|
||||
ax_mojri = serializers.ImageField(required=False)
|
||||
ax_meli = serializers.ImageField(required=False)
|
||||
ax_fani = serializers.ImageField(required=False)
|
||||
ax_sayer = serializers.ImageField(required=False)
|
||||
|
||||
def patch(self, request):
|
||||
user_id = request.user.id
|
||||
serializer = self.InputSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
update_userprofile(user_id, **serializer.validated_data)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class RegisterUserView(APIView):
|
||||
class InputSerializer(serializers.Serializer):
|
||||
name = serializers.CharField()
|
||||
mobile = serializers.CharField()
|
||||
|
||||
def post(self, request):
|
||||
serializer = self.InputSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
name = serializer.validated_data.get('name')
|
||||
mobile = serializer.validated_data.get('mobile')
|
||||
|
||||
user_mojri = Cuser.objects.create_user(
|
||||
username=mobile,
|
||||
password="Jv123456!@#$",
|
||||
)
|
||||
user_mojri.name_family = name
|
||||
user_mojri.mobile = mobile
|
||||
user_mojri.is_active = True
|
||||
user_mojri.save()
|
||||
|
||||
user_profile = UserProfileModel.objects.create(
|
||||
cuser=user_mojri,
|
||||
codemeli=1111111,
|
||||
address='آدرس',
|
||||
codeposti=87878787
|
||||
)
|
||||
|
||||
|
||||
|
||||
user_profile.save()
|
||||
|
||||
|
||||
try:
|
||||
message = f'شرکت لوله گستر گلپایگان: مجری محترم، با سپاس از عضویت شما در باشگاه مجریان، خواهشمندیم به منظور بهرهمندی کامل از امتیازات، پس از ورود به باشگاه مجریان نسبت به تکمیل اطلاعات پروفایل خود اقدام فرمایید.'
|
||||
task_send_sms.apply_async(kwargs={'message': message, 'mobile': str(user_mojri.mobile)}, countdown=60)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
return Response(status=status.HTTP_201_CREATED)
|
||||
Reference in new issue
Block a user