Skip to content

fix&feat: replace logic, add crud for user#3

Open
Sn1kls wants to merge 3 commits into
mainfrom
library-app
Open

fix&feat: replace logic, add crud for user#3
Sn1kls wants to merge 3 commits into
mainfrom
library-app

Conversation

@Sn1kls

@Sn1kls Sn1kls commented Feb 21, 2025

Copy link
Copy Markdown
Owner
  • replace logic(views, serializers) for user
  • add crud for user
  • change poetry

@Sn1kls Sn1kls added bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request labels Feb 21, 2025
url = validated_data.get("url")
try:
book, created = scrape_book_info(url)
except Exception as e:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sn1kls Try moving validation to the validate_url or validate method. Also, instead of returning a generic 'Error scraping book,' try to provide a more specific and understandable error message (e.g., 'Book not found,' 'Invalid site URL,' etc.). If possible, the create method should operate only with already validated data, as long as it does not affect system optimization.

else ""
)

default_category, _ = Category.objects.get_or_create(name="Scraped")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sn1kls Are you sure that you have only one category - "Scraped" ? As I understand you have to get category from response

title=scraped_title,
category=default_category,
defaults={
"author": "Unknown",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sn1kls Check this hardcoded value. Mb u have to get it from response?

email = models.EmailField(unique=True)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
username = models.CharField(max_length=15, unique=True)
extra_fields = models.JSONField(default=dict)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sn1kls Why do you need this field?

),
path("register/", UserRegistrationView.as_view(), name="user-register"),
path("profile/<uuid:id>/", UserRetrieveUpdateView.as_view(), name="user-retrieve"),
path(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sn1kls I think that you can use router here instead of use one path for 3 different methods

class Meta:
model = User
fields = ["id", "username", "email"]
fields = ["id", "username", "email", "extra_fields", "is_staff", "is_superuser"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sn1kls Add validate method to check data for user updating method

try:
uid = force_str(urlsafe_base64_decode(uidb64))
self.user = User.objects.get(pk=uid)
except (TypeError, ValueError, User.DoesNotExist):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sn1kls Split these errors:

User.DoesNotExist should return a 404 error when something is not found.
For other errors related to user-entered data, use a 400 error as a generic client error status.

fields = ["id", "username", "email"]
fields = ["id", "username", "email", "extra_fields", "is_staff", "is_superuser"]

def __init__(self, *args, **kwargs):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sn1kls Instead of using __init__, you can use the standard serializer to_representation method, which should work the same way in this case.

)
def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
permission_classes = [IsOwnerOrAdmin]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sn1kls Add here http_method_names parameter to easier check which request methods this endoint is using to handle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants