fix&feat: replace logic, add crud for user#3
Conversation
| url = validated_data.get("url") | ||
| try: | ||
| book, created = scrape_book_info(url) | ||
| except Exception as e: |
There was a problem hiding this comment.
@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") |
There was a problem hiding this comment.
@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", |
There was a problem hiding this comment.
@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) |
| ), | ||
| path("register/", UserRegistrationView.as_view(), name="user-register"), | ||
| path("profile/<uuid:id>/", UserRetrieveUpdateView.as_view(), name="user-retrieve"), | ||
| path( |
There was a problem hiding this comment.
@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"] |
There was a problem hiding this comment.
@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): |
There was a problem hiding this comment.
@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): |
There was a problem hiding this comment.
@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] |
There was a problem hiding this comment.
@Sn1kls Add here http_method_names parameter to easier check which request methods this endoint is using to handle
Uh oh!
There was an error while loading. Please reload this page.