Skip to content

Commit

Permalink
Getting error creating user
Browse files Browse the repository at this point in the history
  • Loading branch information
MdSamsuzzohaShayon committed Mar 6, 2024
1 parent 79411f0 commit d9d8c2b
Show file tree
Hide file tree
Showing 45 changed files with 376 additions and 257 deletions.
3 changes: 2 additions & 1 deletion client/components/article/ArticleAdd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ const handleArticleAdd = async (e: Event) => {
title: articleState.title,
content: state.content,
thumbnail: null, // You may need to handle thumbnail separately based on your requirements
authorId: articleState.author,
// authorId: articleState.author,
authorId: 1,
categoryId: articleState.category,
},
};
Expand Down
26 changes: 26 additions & 0 deletions server/READNE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Backend
- Register user mutation
```
mutation RegisterUser($email: String!, $password: String!,
$firstName: String!, $lastName: String!, $birth: String){
registerUser(email: $email, password: $password,
firstName: $firstName, lastName: $lastName, birth: $birth){
user{
id
password
lastLogin
birth
}
}
}
```

```
{
"email": "[email protected]",
"password": "Test1234",
"firstName": "fn1",
"lastName": "ln1",
"birth": "2024-03-06T11:26:12.306Z"
}
```
File renamed without changes.
5 changes: 5 additions & 0 deletions server/account/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin
from account.models import User

# Register your models here.
admin.site.register(User)
6 changes: 6 additions & 0 deletions server/account/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AccountConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'account'
73 changes: 73 additions & 0 deletions server/account/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, AbstractUser


class UserManager(BaseUserManager):
def create_user(self, email, birth=None, password=None):
if not email:
raise ValueError("Users must have an email address")

user = self.model(
email=self.normalize_email(email),
birth=birth,
)

user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, email, birth=None, password=None):
user = self.create_user(
email,
password=password,
birth=birth,
)
user.is_admin = True
user.save(using=self._db)
return user


class User(AbstractUser):
email = models.EmailField(
verbose_name="email address",
max_length=255,
unique=True,
)
birth = models.DateField()
is_active = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)

groups = models.ManyToManyField(
'auth.Group',
verbose_name='groups',
blank=True,
help_text='The groups this user belongs to.',
related_name='user_groups',
)

# Provide a unique related_name for the user_permissions field
user_permissions = models.ManyToManyField(
'auth.Permission',
verbose_name='user permissions',
blank=True,
help_text='Specific permissions for this user.',
related_name='user_permissions_account', # Unique related_name to resolve the clash
)

objects = UserManager()

USERNAME_FIELD = "email"
REQUIRED_FIELDS = []

def __str__(self):
return self.email

def has_perm(self, perm, obj=None):
return True

def has_module_perms(self, app_label):
return True

@property
def is_staff(self):
return self.is_admin
43 changes: 43 additions & 0 deletions server/account/resolvers/mutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import graphene
from django.contrib.auth import get_user_model
from .queryTypes import UserType
from account.models import User
from datetime import datetime

class UserRegistrationMutation(graphene.Mutation):
user = graphene.Field(UserType)

class Arguments:
email = graphene.String(required=True)
password = graphene.String(required=True)
first_name = graphene.String(required=True)
last_name = graphene.String(required=True)
birth = graphene.String() # Accept birthdate as a string in ISO 8601 format

def mutate(self, info, email, password, first_name, last_name, birth=None):
birthdate = None
if birth:
# Parse the birthdate string into a datetime object
birthdate = datetime.fromisoformat(birth.replace('Z', '+00:00'))

# Create the user object
user = User(
email=email,
birth=birthdate, # Replace 'birthdate' with the actual field in your model
is_active=False,
username=email, # Set username directly
first_name=first_name,
last_name=last_name,
)

# Set the password
user.set_password(password)

# Save the user
user.save()

# Return the mutation result
return UserRegistrationMutation(user=user)

class Mutation(graphene.ObjectType):
register_user = UserRegistrationMutation.Field()
7 changes: 7 additions & 0 deletions server/account/resolvers/queryTypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from account.models import User
from graphene_django.types import DjangoObjectType
import graphene

class UserType(DjangoObjectType):
class Meta:
model = User
3 changes: 3 additions & 0 deletions server/account/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions server/account/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
76 changes: 0 additions & 76 deletions server/blog/migrations/0001_initial.py

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions server/blog/migrations/0003_article_link.py

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions server/blog/migrations/0005_article_thumbnail.py

This file was deleted.

Loading

0 comments on commit d9d8c2b

Please sign in to comment.