created an api and added swagger to the api with the help of the package
drf-yasg
the current updated version 1.20.0, then added code like this
success_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='200'), 'success': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})
error_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='400'), 'error': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})
class TestView(APIView):
api_view = ['POST']
authentication_classes = [SessionAuthentication, TokenAuthentication]
invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)
#swagger_auto_schema(
manual_parameters=[invitation_file], operation_description="description",
responses={200: success_res_data, 400: error_res_data}
)
def post(self, request):
invitation_file = request.data.get('invitation_file', None)
invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)
#swagger_auto_schema(
manual_parameters=[invitation_file], operation_description="description",
responses={200: success_res_data, 400: error_res_data}
)
def post(self, request):
invitation_file = request.data.get('invitation_file', None)
this invitation_file variable is returning None even if we pass the file from front-end
after a little research and checking the same api in postman, changed the code from whats above to
success_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='200'), 'success': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})
error_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='400'), 'error': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})
class TestView(APIView):
api_view = ['POST']
authentication_classes = [SessionAuthentication, TokenAuthentication]
invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)
#swagger_auto_schema(
manual_parameters=[invitation_file],operation_description="API Description", consumes="multipart/form-data",
responses={200: success_res_data, 400: error_res_data}
)
def post(self, request):
invitation_file = request.data.get('invitation_file', None)
invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)
#swagger_auto_schema(
manual_parameters=[invitation_file], operation_description="description",
responses={200: success_res_data, 400: error_res_data}
)
def post(self, request):
invitation_file = request.data.get('invitation_file', None)
now click on the unlock button on the right of the api and add the "Token auth-token" and click authenticate
now after calling the api and using pdb the value of the passed file is shown for the variable invitation_file
Related
How could i achieve email functionality using drf as backeend and django to hit these apis.What i need how will user be confirm from django while using drf to send activation link.
At first, you need to add the code to send the verification email when you register.
from base64 import urlsafe_b64decode, urlsafe_b64encode
from django.contrib.auth.tokens import default_token_generator
from django.template.loader import render_to_string
from threading import Thread
class EmailRegisterView(APIView):
"""APIs for Email Registeration"""
permission_classes = [AllowAny]
def post(self, request):
"""Signup with Email"""
serializer = EmailRegisterSerializer(data=request.data)
if serializer.is_valid():
...
user.save()
// send verification link
cur_token = default_token_generator.make_token(user)
email = urlsafe_b64encode(str(user.email).encode('utf-8'))
# now send email
mail_subject = 'Email Confirmation'
message = render_to_string('emails/email_verification.html', {
'site_url': settings.SITE_URL,
'token': f'api/users/verify/{email.decode("utf-8")}/{cur_token}',
})
t = Thread(target=send_mail, args=(
mail_subject, message, settings.EMAIL_FROM_USER, to_email))
t.start()
return Response({
"success": True,
"user": MemberSerializer(user).data
}, status.HTTP_200_OK)
And you can add the confirmation view.
urlpatterns = [
...
path('verify/<str:email>/<str:email_token>',
verify_email, name="verify_token"),
...
]
Then the verify_email function verifies the token and redirects.
#api_view(['GET'])
#permission_classes([AllowAny])
def verify_email(request, email, email_token):
"""Verify Email"""
try:
target_link = settings.CLIENT_URL + "/account/result?type=email_verified"
if verify_token(email, email_token):
return redirect(target_link)
else:
return render(
request,
"emails/email_error.html",
{'success': False, 'link': target_link}
)
except BaseException:
pass
Here is the verify_token function.
def verify_token(email, email_token):
"""Return token verification result"""
try:
users = Member.objects.filter(
email=urlsafe_b64decode(email).decode("utf-8"))
for user in users:
valid = default_token_generator.check_token(user, email_token)
if valid:
user.is_verified = True
user.save()
return valid
except BaseException:
pass
return False
Hi everyone I integrate my Django Web app with mail chimp . in my admin panel when I open marketing preference it give me error . and it do not subscribe my users when I click on subscribe my user remain unsubscribe when i hit save .the error I got is
{'type': 'https://mailchimp.com/developer/marketing/docs/errors/', 'title': 'Invalid Resource', 'status': 400, 'detail': "The resource submitted could not be validated. For field-specific details, see the 'errors' array.", 'instance': '2b647b4f-6e58-439f-8c91-31a3223600a9', 'errors': [{'field': 'email_address', 'message': 'This value should not be blank.'}]}
my models.py file is:
class MarketingPreference(models.Model):
user =models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
subscribed =models.BooleanField(default=True)
mailchimp_subscribed = models.NullBooleanField(blank=True)
mailchimp_msg =models.TextField(null=True , blank=True)
timestamp =models.DateTimeField(auto_now_add=True)
updated =models.DateTimeField(auto_now=True)
def __str__(self):
return self.user.email
def marketing_pref_create_reciever(sender, instance, created, *args, **kwargs):
if created:
status_code, response_data = Mailchimp().subscribe(instance.user.email)
print(status_code, response_data)
post_save.connect(marketing_pref_create_reciever, sender=MarketingPreference)
def marketing_pref_update_reciever(sender, instance, *args, **kwargs):
if instance.subscribed != instance.mailchimp_subscribed:
if instance.subscribed:
#subscribing user
status_code, response_data = Mailchimp().subscribe(instance.user.email)
else:
#unsubscribing user
status_code, response_data = Mailchimp().unsubscribe(instance.user.email)
if response_data['status'] =='subscribed':
instance.subscribed = True
instance.mailchimp_subscribed = True
instance.mailchimp_msg = response_data
else:
instance.subscribed = False
instance.mailchimp_subscribed = False
instance.mailchimp_msg = response_data
pre_save.connect(marketing_pref_update_reciever, sender=MarketingPreference)
def make_marketing_pref_reciever(sender, instance, created, *args, **kwargs):
if created:
MarketingPreference.objects.get_or_create(user=instance)
post_save.connect(make_marketing_pref_reciever , sender=settings.AUTH_USER_MODEL)
my utils.py is:
MAILCHIMP_API_KEY = getattr(settings, "MAILCHIMP_API_KEY" , None)
MAILCHIMP_DATA_CENTER = getattr(settings, "MAILCHIMP_DATA_CENTER" , None)
MAILCHIMP_EMAIL_LIST_ID = getattr(settings, "MAILCHIMP_EMAIL_LIST_ID" , None)
def check_email(email):
if not re.match(r".+#.+\..+",email):
raise ValueError("String passed is not a valid email address")
return email
def get_subscriber_hash(member_email):
#check email
check_email(member_email)
member_email = member_email.lower().encode()
m = hashlib.md5(member_email)
return m.hexdigest()
class Mailchimp(object):
def __init__(self):
super(Mailchimp, self).__init__()
self.key = MAILCHIMP_API_KEY
self.api_url = "https://{dc}.api.mailchimp.com/3.0/".format(dc=MAILCHIMP_DATA_CENTER)
self.list_id = MAILCHIMP_EMAIL_LIST_ID
self.list_endpoint = '{api_url}/lists/{list_id}'.format(api_url=self.api_url, list_id=self.list_id)
def get_members_endpoint(self):
return self.list_endpoint + "/members"
def change_subscription_status(self, email, status='unsubscribed'):
hashed_email = get_subscriber_hash(email)
endpoint = self.get_members_endpoint() +"/" + hashed_email
data = {
"status":self.check_valid_status(status)
}
r = requests.put(endpoint, auth=("",self.key), data=json.dumps(data))
return r.status_code, r.json()
def check_subscription_status(self,email):
hashed_email = get_subscriber_hash(email)
endpoint = self.get_members_endpoint() +"/" + hashed_email
r = requests.get(endpoint, auth=("", self.key))
return r.status_code, r.json()
def check_valid_status(self, status):
choices = ['subscribed' , 'unsubscribed', 'cleaned' , 'pending']
if status not in choices:
raise ValueError("not a valid choice for email status")
return status
def add_email(self,email):
status = "subscribed"
self.check_valid_status(status)
data = {
"email_address":email,
"status": status
}
endpoint = self.get_members_endpoint()
r = requests.post(endpoint, auth=("",self.key), data=json.dumps(data))
return self.change_subscription_status(email, status='subscribed')
def unsubscribe(self, email):
return self.change_subscription_status(email, status='unsubscribed')
def subscribe(self, email):
return self.change_subscription_status(email, status='subscribed')
def pending(self, email):
return self.change_subscription_status(email, status='pending')
mixins.py is:
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
class CsrfExemptMixin(object):
#method_decorator(csrf_exempt)
def dispatch(self, request,*args, **kwargs):
return super(CsrfExemptMixin, self).dispatch(request,*args, **kwargs)
and my views.py is:
from .mixins import CsrfExemptMixin
from .models import MarketingPreference
from . utils import Mailchimp
MAILCHIMP_EMAIL_LIST_ID = getattr(settings, "MAILCHIMP_EMAIL_LIST_ID" , None)
# Create your views here.
class MarketingPreferenceUpdateView(SuccessMessageMixin, UpdateView):
form_class = MarketingPreferenceForm
template_name = 'base/forms.html'
success_url = '/settings/email/' #this is a builtin method and by default it will go to marketig preference
success_message = 'Your email preferences have been updated. Thank you'
def dispatch(self, *args, **kwargs): #when user came with incognito email will not show to him it will redirect him back to login page
user = self.request.user
if not user.is_authenticated:
return redirect("/login/?next=/settings/email/") #HttpResponse("not allowed", status=400)
return super(MarketingPreferenceUpdateView, self).dispatch(*args,**kwargs)#(request, *args...)
def get_context_data(self, *args, **kwargs):
context = super(MarketingPreferenceUpdateView, self).get_context_data(*args,**kwargs)
context['title'] = 'Update Email Preference'
return context
def get_object(self):
user = self.request.user
obj , created = MarketingPreference.objects.get_or_create(user=user)
return obj
class MailchimpWebhookView(CsrfExemptMixin,View): #it will not work because our web application is not deployes yet and webhook mailchimp do not work with local host
#def get(self, *args, **kwargs):
# return HttpResponse('thank you', status=200)
def post(self, request, *args, **kwargs):
data = request.POST
list_id = data.get('data[list_id]')
if str(list_id) == str(MAILCHIMP_EMAIL_LIST_ID): # I CHECK THAT DATA DATA IS THE RIGHT LIST
hook_type = data.get("type")
email = data.get('data[email]')
response_status, response = Mailchimp().check_subscription_status(email)
sub_status = response['status']
is_subbed = None
mailchimp_subbed = None
if sub_status == "subscribed":
is_subbed, mailchimp_subbed = (True, True)
elif sub_status == "unsubscribed":
is_subbed, mailchimp_subbed = (False, False)
if is_subbed is not None and mailchimp_subbed is not None:
qs = MarketingPreference.objects.filter(user__email__iexact=email)
if qs.exists():
qs.update(subscribed=is_subbed, mailchimp_subscribed=mailchimp_subbed, mailchimp_msg=str(data))
return HttpResponse('thank you', status=200)
Here you have already assigned request.POST to data which is now a dictonary, so to get a value from dictonary you should use the field name of the form widget, as data == request.POST now.
Problem is you are getting wrong key.So your email will always be empty
list_id = data.get('data[list_id]')
email = data.get('data[email]')#You are getting wrong key
It should be like this
list_id = data.get('list_id')
email = data.get('email')
Good day guy i'm working on an drf api endpoint that requires user uploading image and text to the same endpoint, i've done all that is required but i still keep getting error, below is snippet of my code and error msg
APIVIEW
class CreateProfileView(APIView):
parser_classes = (MultiPartParser,)
serializer_class = schoolProfileSerializer
queryset = schoolProfile.objects.all()
permission_classes = [permissions.AllowAny]
def perform_create(self, serializer):
serializer.save(user=self.request.user)
def post(self, request):
file_upload = schoolProfileSerializer(data =request.data, instance=request.user)
if file_upload.is_valid():
file_upload.save()
return Response(file_upload.data, status=status.HTTP_201_CREATED)
else:
return Response(file_upload.errors, status=status.HTTP_400_BAD_REQUEST )
SERIALIZER
class Base64Imagefield(serializers.ImageField):
def to_internal_value(self, data):
if isinstance(self, six.string_types):
if 'data: ' in data and ';base64, ' in data:
header, data = data.split(';base64,')
try:
decode_file = base64.b64decode(data)
except TypeError:
self.fail('invalide image')
file_name = str(uuid.uuid4())[:16]
file_extension = self.get_file_extension(file_name, decode_file)
complete_file_name = "%s.%s" %(file_name, file_extension)
data = ContentFile(decode_file, name=complete_file_name)
return super(Base64Imagefield, self).to_internal_value(data)
def get_file_extension(self, file_name, decode_file):
extension = imghdr.what(file_name, decode_file)
extension = 'jpg' if extension == 'jpeg' else extension
return extension
class schoolProfileSerializer(serializers.ModelSerializer):
parser_classes = (MultiPartParser, FormParser, )
id = serializers.IntegerField(source='pk', read_only=True)
email = serializers.CharField(source='user.email', read_only=True)
username = serializers.CharField(source='user.username', read_only=True)
badge = Base64Imagefield(max_length=None, use_url=True)
class Meta:
model = schoolProfile
fields = ( 'email', 'id', 'username', 'school_name',
'address', 'badge', 'gender', 'level',
)
def create(self, validated_data, instance=None):
if 'user' in validated_data:
user = validated_data.pop('user')
else:
user = CustomUser.objects.create(**validated_data)
profile, created_profile = schoolProfile.objects.update_or_create(user=user,
**validated_data)
return profile
Angular service
postSchoolProfile(profile: schoolProfile):Observable<schoolProfile>{
const url= `${environment.mainUrl}/school-profile/create`
return this.httpClient.post<schoolProfile>(url, {profile})
}
Error msg
detail "Unsupported media type \"application/json\" in request.
can anyone help out pls ?
Dear community and forum,
I am in charge of developing RESTful API URLs for a website project. However, when it comes to testing I have got that error:
self = HyperlinkedIdentityField('api:request')
value = <ResourceRequest: JoKLLwwKqxrkrwWmcjOWzIscGzpsbgWJqRAOZabnwxQpiEDRfifeZhvzpRRp...ewyOFcaQVhchYNVIhUoiWBzKMrFYvYQBMNRZsLFfOZSjclHUXwyXZQHxjMtbHvWefMIlyZqvTvXqiu>
def to_representation(self, value):
assert 'request' in self.context, (
"`%s` requires the request in the serializer"
" context. Add `context={'request': request}` when instantiating "
"the serializer." % self.__class__.__name__
)
E AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context. Add `context={'request': request}` when instantiating the serializer.
/usr/lib/python2.7/site-packages/rest_framework/relations.py:351: AssertionError
The serialiser is (sorry for the ugly code so far):
class ResourceRequestSerializer(serializers.ModelSerializer):
# context = self.kwargs.get('context', None)
# request = kwargs['context']['request']
# print(kwargs)
view_name = 'api:request'
url = serializers.HyperlinkedIdentityField(view_name)
# print(self)
# url = URLField(view_name=view_name, read_only=True, many=True)
# url = serializers.SerializerMethodField()
support_level = serializers.SerializerMethodField()
originator = BriefUCLProfileSerializer(source='originator.ucl_profile')
sponsor = BriefUCLProfileSerializer()
versioned_dependencies = serializers.StringRelatedField(many=True)
types_of_work = serializers.StringRelatedField(many=True)
previous = serializers.SerializerMethodField()
status_history = RequestStatusChangeSerializer(many=True)
For the test:
# Change the status through a POST request
response = self.app.post(
reverse(
'api:request',
args=[request1.pk],
),
params={
'context': request1,
'format': 'json',
'status': ResourceRequest.STATUS_APPROVED,
},
# context=request1,
headers=self.auth_headers,
)
I am still wondering if the context has to be passed from within the serialiser or from the test.
Here is the view too:
class ResourceRequestAPIView(RetrieveAPIView):
"""Retrieve an individual resource request by pk and optionally update status"""
serializer_class = ResourceRequestSerializer
permission_classes = (IsAuthenticated,)
authentication_classes = (TokenAuthentication, SessionAuthentication)
def get_object(self):
try:
return ResourceRequest.objects.get(pk=self.kwargs.get('pk'))
except ResourceRequest.DoesNotExist:
raise Http404
def post(self, request, *args, **kwargs):
resource_request = self.get_object()
status = request.data['status']
if status != ResourceRequest.STATUS_SUBMITTED:
# Change the status
resource_request.set_status(status,
request.user)
# And send emails
request_url = request.build_absolute_uri(
reverse('api:request', args=[resource_request.pk])
)
send_emails(resource_request,
request_url=request_url,
)
serializer = ResourceRequestSerializer(resource_request)
return Response(serializer.data)
Any help greatly appreciated !
Thank you
Roland
Right, so with that viewset in hand, you'll have to initialize the serializer with the context:
serializer = ResourceRequestSerializer(
resource_request,
context=self.get_serializer_context(),
)
get_serializer_context() is provided by default by DRF viewsets.
As i am using a third party package called djoser to handle the token authentication. i want to customise the response. But after trying to change it, the result is not what i wanted.
I just wanted to get the token value of the token instead of having "auth_token:" in front.
Here is the link to djoser: https://github.com/sunscrapers/djoser
Here is my code :
serializer.py
class TokenCreateSerializer(serializers.Serializer):
password = serializers.CharField(
required=False, style={'input_type': 'password'}
)
default_error_messages = {
'invalid_credentials': constants.INVALID_CREDENTIALS_ERROR,
'inactive_account': constants.INACTIVE_ACCOUNT_ERROR,
}
def __init__(self, *args, **kwargs):
super(TokenCreateSerializer, self).__init__(*args, **kwargs)
self.user = None
self.fields[User.USERNAME_FIELD] = serializers.CharField(
required=False
)
def validate(self, attrs):
self.user = authenticate(
username=attrs.get(User.USERNAME_FIELD),
password=attrs.get('password')
)
self._validate_user_exists(self.user)
self._validate_user_is_active(self.user)
return attrs
def _validate_user_exists(self, user):
if not user:
self.fail('invalid_credentials')
def _validate_user_is_active(self, user):
if not user.is_active:
self.fail('inactive_account')
views.py
class TokenCreateView(utils.ActionViewMixin, generics.GenericAPIView):
"""
Use this endpoint to obtain user authentication token.
"""
serializer_class = settings.SERIALIZERS.token_create
permission_classes = [permissions.AllowAny]
def _action(self, serializer):
token = utils.login_user(self.request, serializer.user)
token_serializer_class = settings.SERIALIZERS.token
return Response(
data=token_serializer_class(token).data,
status=status.HTTP_200_OK,
)
my custom views.py
class CustomTokenCreateView(utils.ActionViewMixin, generics.GenericAPIView):
"""
Use this endpoint to obtain user authentication token.
"""
serializer_class = TokenCreateSerializer
permission_classes = [permissions.AllowAny]
def _action(self, serializer):
token = utils.login_user(self.request, serializer.user)
token_serializer_class = settings.SERIALIZERS.token
content = {
'Token': token_serializer_class(token).data,
'promptmsg': 'You have successfully login',
'status': '200'
}
return Response(
data=content,
status=status.HTTP_200_OK,
)
Result from djoser token authentication:
Success :
{
"auth_token": "da57cd11c34cb4332eaa6cc2cac797d0ee95cafb"
}
Result from my edited djoser authentication
Success:
{
"Token": {
"auth_token": "da57cd11c34cb4332eaa6cc2cac797d0ee95cafb"
},
"promptmsg": "You have successfully login",
"status": "200"
}
The result i want
Success:
{
"Token": "da57cd11c34cb4332eaa6cc2cac797d0ee95cafb",
"promptmsg": "You have successfully login",
"status": "200"
}
Is there a way to remove the auth_token tag ? i do not mind if its auth_token but as long as the format is what it is expected
Try this to access the value:
content = {
'Token': token_serializer_class(token).data["auth_token"],
'promptmsg': 'You have successfully login',
'status': '200'
}
That way you are not assigning the entire object.