Jsoup in CFscript execute connection as POST - coldfusion

The page I want to parse could be get only by POST method.
This is easy for Java as I can see:
import org.jsoup.Jsoup;
Response res = Jsoup.connect("URL").method(Method.POST).execute();
Document doc = res.parse();
I could not produce the same thing using CFscript.
jsoup = createObject("java", "org.jsoup.Jsoup");
response = jsoup.connect("URL").method(Method.POST).execute();
if (response.statusCode() == 200)
{
doc = response.parse();
}
-ERR Element POST is undefined in METHOD
I tried almost everything.
I was unable to use .method() and .execute() at the same time.
If I call .get() or .post() directly I can not check statusCode() back then.

If you look at the API, Method is another JSoup class. You need to create an instance of that class before you can access the POST constant. Also, Method is a little different than your typical java class. It is an enum (or constant). Those are essentially handled as inner classes, which require a special syntax with createObject:
methodClass = createObject("java", "org.jsoup.Connection$Method");
response = jsoup.connect("http://example.com").method(methodClass.POST).execute();

Alternatively, you can invoke the post() method directly:
response = jsoup.connect("URL").post();

Related

Which method of 'Django Serializer' do I need to customize?

The frontend sends the json in an array.
I received the value with difficulty as shown in ex) below.
ex)
applier_phone = data.get('phone')[0]
applier_name = data.get('name')[0]
applier_birth = data.get('birth')[0]
applier_gender = data.get('gender')[0]
But I want to get the value using Serializer.
In this case, which method of Serializer do I need to customize?
I don't think the best solution would be to modify the Serializer in itself. Rather i would suggest to modify the Json Object so the serializer detects it as by the initial schema od your Model.
This will let the Serializer still be usable for multiple case and instances.
If you still want to take the first approach i would suggest you may intervene in any of the .create() .update() , or even validate() methods for re-structuring your JSON object.

Choosing correct Django delete view approach

I'm working on Django website and I have problem in figuring out correct/good way to handle delete view. From what I found out there are two ways to approach it:
1
class ObjectDeleteView(DeleteView):
model = Object
def get_success_url(self):
objectid = self.kwargs['object_id']
object = Object.objects.get(id = objectid)
container = object.container
containerid = container.id
url = reverse('Containers:showContainerContent', args=[containerid])
return url
def get_object(self):
return get_object_or_404(Object, pk=self.kwargs['object_id'])
2
def objectDelete(request, object_id):
object = Object.objects.get(id = object_id)
container = object.container
containerid = container.id
url = reverse('Containers:showContainerContent', args=[containerid])
return HttpResponseRedirect(url)
From what I can tell both are doing exactly the same thing - once object is deleted present user with page under Containers:showContainerContent.
The big difference I am experiencing is error I am getting when running unit test for this (simple call of the website and check of response code). With option 1 I end up getting error
django.template.exceptions.TemplateDoesNotExist: ContainerApp/object_confirm_delete.html
Which I understand - I don't have this template, this is default template for DeleteView, hence error is correct. The thing is I don't want to have any extra page. Just redirect user and that's it.
Also, I tested changing return url to return HttpResponseRedirect(url) in option 1, but result is the same.
What should I do here? Should I just continue with option 2? What are or might be the drawbacks for this approach?
There is a major difference between two class based delete view and function based view (the way you declared it).
CBV accepts get, post and delete http methods. When you send a get request to class based view, it does not delete the object. Instead it renders template with object to be deleted in context. This is basically used to have confirmation. For example you can send a get request and it will render a template with text "Do you really want to delete?" or "Please confirm blah blah..". And if you send a post or delete request, it will actually delete the object and redirect to next page.
FBV, on the other hand, give you full control over what you want to do. And as you declared it, it will accept any request type and delete the object and redirect to next page because you have not done any request type check in your view which is not a great idea IMHO. You should not allow deletion on get requests. They should be idempotent. There are plenty of otherthings that CBV provides. For example in case the object does not exist your FBV will crash. CBV, on contrary, will return proper 404 response if object does not exist.
So I think there is no bad in using FBV, but make is strong and secure enough that it handles every case (what if object does not exist?, what about confirmation?, GET should be idempotent only allow deletion with post? etc etc). Or simply use CBV.

Django creating model instances - Validation before creation? In manager class? What is manager class for?

Say I have a class "Book" and I want to hit an API to verify the book exists before creating my model.
Do I create my "BookManager" class, override create, hit the api, and throw an exception if not valid or create if valid?
Then in Book I'd write objects = BookManager()
And create a book with.
new_book = Book.objects.create(name)?
Basically, this feels like a good way to organize my code, but I'm not sure if this is intended use for the Manager class as opposed to only modifying the queryset.
Additionally, does anyone have a good reference on how to structure your django rest framework app? Folder structure etc
I will start with very basics. I am assuming you api calls are simple get requests for now which you can achieve with python http package.
(I am assuming the api is a third party api for now)
you can define a simple view let say view name be : bookM
Next you have defined you model with lets say a primary key, book_name, other_attrs, date
now when you hit you api within this view you can get the response from get request
requests.get(url = URL, params = PARAMS)
With this if you find the response sent back with some text or null you can act on model as below :
book= BookSave(
name = "book1",
)
book.save()
If this is not the case you can save error message in python variable and display while rendering html
You can use this view as api and do ajax call from web page, as well in this case you can just return back messages

Passing GET params to a Django View internally

I am trying to call a Django view internally from another view:
response = BlogViewSet.as_view({'get':'list'})(request)
BlogViewSet is actually a rest framework view.
The above code works and I can access response.data but what I actually want to do is pass in some GET params to do some filtering. I tried the following but it didn't work:
response = BlogViewSet.as_view({'get':'list'})(request, my_param=something)
I realise I could modify request to add GET params but it seems wrong to modify it as it might be used later in the view.
You shouldn't ever call the view itself form another view.
You should instead try to extract the meaningful data / code out of the BlogViewSet view and call them directly from the various views.
Calling one view from another is a bad practice.
Why not request url of the view instead of calling the view itself.
r = requests.get("<url_to_access_view>", params={})

Django passing parameter with AJAX

I am trying to pass parameters to Django view but I couldn't decide what is the best way to do it.
I am making an AJAX call and my javascript code is
url = "/review/flag/code/"+ code +"/type/" + type + "/content/" + content + "/";
$.getJSON(url, somefunction);
The corresponding url for this call
(r'^rate/code/(?P<code>[-\w]+)/type/(?P<type>[-\w]+)/content/(?P<content>[-\w]+)/$', 'project.views.flag_review'),
And I can get the parameters in my view such that
def rate_review(request,code,type,content):
....
My question is because content comes from a textarea it may involve many escape chars and passing it like above causes problems due to regular expression.
Is there any way if I pass the parameters like www.xx.com/review/rate?code=1&type=2&content=sdfafdkaposdfkapskdf... ?
Thanks
If the content variable is input through a textarea input field in a form, you should probably be using the HTTP POST method for submitting the data. You would get something like:
url = "/review/flag/code/"+ code +"/type/" + type;
$.post(url, {'content': content}, somefunction, 'html');
(r'^rate/code/(?P<code>[-\w]+)/type/(?P<type>[-\w]+)/$', 'project.views.flag_review'),
def rate_review(request,code,type):
content = request.POST['content']
...
Sure, in your rate_review function you can access request.GET and access those paramters:
/rate/code/?foo=bar
def rate_review(request):
request.GET['foo']