Using the Django API - django

First of all, any help with this would be amazing! I am currently trying to learn Django and am also fairly new to Python. I am trying to create a simple project management web service that could communicate using JSON with another application. All it needs to do is:
Give a list of all projects.
Give a list of all tasks for each of the projects.
Allow users to submit time for a project and task.
For models I currently have a 'Project' model and 'Task' model. I have filled the database with some dummy information. My project model has variables id, name, and description. Task just has project, task_name.
My view for task 1 above is
def api(request):
projects = Project.objects.all()#.order_by('id')
projects = serializers.serialize('json', projects, indent=2)
return HttpResponse(projects, mimetype='application/json')
associated url is url(r'^api/project/$', 'project.views.api'),
However this outputs the text below when typing
http://127.0.0.1:8000/api/project/
into the browser. Is there a better way to test JSON output? I would rather it only output the 'id' and 'name'. For some reason the id doesn't even show. This is what I put for id in models id = models.AutoField(primary_key=True). It works in the interactive shell.
[
{
"pk": 1,
"model": "project.project",
"fields": {
"name": "Project One",
"description": "This is the description for project one"
}
},
{
"pk": 2,
"model": "project.project",
"fields": {
"name": "Project Two",
"description": "This is Project Two"
}
}
]
I learn by example so if someone could show me how this should be done I would be extremely grateful!
By the way for outputting the list of tasks, I was thinking the url would be something like:
http://127.0.0.1:8000/api/project/?project_name
but I'm not sure how to handle that in the view.

Related

Update embedded models in Loopback 4

I have the following model relationship in my LoopBack 4 application:
A Survey has 1-many relationship with Question,
Question has 1-many relationship with Option.
Am using MongoDB as the database. I am unable to figure out how to implement a PUT/PATCH REST api, that can update the entire Survey model containing the Questions and Options. I get the following exception when i try a PATCH on the sample request JSON below:
Exception: Unhandled error in PATCH /5be3e00aad0df83d10e580d1: 500
TypeError: Class constructor Question cannot be invoked without 'new'
at Function.DataAccessObject._coerce (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\dao.js:1799:22)
at doUpdate (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\dao.js:2735:20)
at C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\dao.js:2714:11
at doNotify (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
at doNotify (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
at Function.ObserverMixin._notifyBaseObservers (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:178:5)
at Function.ObserverMixin.notifyObserversOf (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)
at Function.ObserverMixin._notifyBaseObservers (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:176:15)
at Function.ObserverMixin.notifyObserversOf (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)
at C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\dao.js:2699:11
at doNotify (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
at doNotify (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
at Function.ObserverMixin._notifyBaseObservers (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:178:5)
at Function.ObserverMixin.notifyObserversOf (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)
at Function.ObserverMixin._notifyBaseObservers (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:176:15)
at Function.ObserverMixin.notifyObserversOf (C:\Users\nk\Documents\Project\backend\surveyService\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)
Sample PATCH request:
[{
"title": "Some survey title 1",
"description": "Some survey title 1",
"questions": [{
"options": [{
"description": "Some option description 1",
"type": "Option"
}],
"description": "Some question description 1"
}]
}]
Have not been able to find any helpful information on this yet. Any pointers please?
Thanks in advance.

Django tests loading fixtures auth.group

When I run django tests I get the error:
IntegrityError: Problem installing fixture ... ContentType matching
query does not exist.: (auth.group:pk=2) field_value was
'[u'add_corsmodel', u'corsheaders', u'corsmodel']'
I get the fixtures by doing
python manage.py dumpdata --natural-foreign --exclude=contenttypes --exclude=auth.Permission
How can I solve this? should I exclude some other table?
well I tried to do one simple thing to add permissions.
I've created a .json file and put in data.
[
{
"model": "auth.group",
"fields": {
"name": "foo",
"permissions": [
29,45,46,47,48 //permission ID's created in auth.group
]
}
},
{
"model": "auth.group",
"fields": {
"name": "new_grp",
"permissions": [
29,45,46,47,48
]
}
}
]
this is my initial permissions that I want to include and then
manage.py loaddata <myJsonFIle>
i think in your case it is not able to find the rows or columns in the table that's why it shows IntegrityError
Removing Group from your fixture resolves your issue
because Group depends on Permission which depends on ContentType, both of which were stripped from the export.
Judging from your comments on this question it sounds like you already figured that part out. There is another answer to you question though: Don't use fixtures for test data in Django. Django's documentation suggests you use the TestCase.setUpTestData method to setup your test data. Here's the exert from the documentation: "Tests are more readable and it’s more maintainable to create objects using the ORM."

Upload photos to Facebook Sales group album with graph API, using the Source field and not URL

Is there a way to create an album to the SALES groups in Facebook ? when I use the graph API explorer to fetch the user's groups it only brings back the user groups and not the sales groups he / she is part of
Command I used is get > v2.7 > me/groups and it returns
{
"data": [
{
"name": "Shadow Creek Columbus Indiana",
"privacy": "SECRET",
"id": "647651662006143"
}
],
"paging": {
"cursors": {
"before": "NjQ3NjUxNjYyMDA2MTQz",
"after": "NjQ3NjUxNjYyMDA2MTQz"
}
}
}
My second question is, if I were to create an album in the above group and add photos to it using the URL I am able to, however I have a phonegap app and that would be loading the images from the local and not from web, so I need to use the Source field instead of URL. However I am not able to test the same using the explorer. What value should I specify in the Source field ?

facebook group members Bio information

I am interested in retrieving a Facebook group members' Bio information.
I have created an app where I have requested permission of about user_about_me and the app is under review for submission.
I have coded for the app in JSP but it can only select name, gender, last_name, first_name only. It is not selecting the Bio information.
I face the same problem even in the graph API explorer, when I use
{group_ID}/members
I can retrieve list of all group members. But I can not see more details than the following:
"data": [
{
"name": "name",
"administrator": false,
"id": "USER ID"
},
..
When I click on the ID (or enter the ID in the explorer), I can only get the following:
{
"id": "member_ID",
"first_name": "firstname",
"last_name": "lastname",
"link": "https://www.facebook.com/app_scoped_user_id/...",
"name": "name ",
"updated_time": "2015-02-07T10:02:58+0000"
}
This is not showing the Bio information.
when I enter the following:
{member_ID}?fields=bio
I just get
{
"id": "10153223503039309"
}
I will appreciate if someone please identify me the problem. I believe this problem will also solve my code problem, where at the moment I am unable to retrieve the group member's Bio information too.
Is it possible that the problem will be resolved after the app's acceptance, which is under review?
Thanks you very much.
Syed
You can´t just grab more information from the user just because he is a member of some group. You would have to let him authorize your App with the correct permissions in order to get more data.

How to handle different data type for same property for a HTTP or REST API

I work on a web application using AngularJS for the frontend and the backend for Symfony2. I want to make a web service so I can have a one page app. I am wondering about how I should handle the data exchange between the two layers.
Consider this use case:
A user wants to create a project in the application and must specify whether it be as a customer or supplier in that project. The creator of the project gives the project a name and a description. Then he enters the email address of the contact person (who will be the customer or supplier in the project depending on the choice of the user who created the project). When creating the project the system checks whether the contact person is already registered otherwise it sends an invitation to join the application and waits. It is not necessary to invite the contact person at the creation of a project that can be done later.
I imagine the API is as follows:
POST http://app.com/api/projects
Request:
{
"name": "My Project"
"descrption": "My Project'description"
"vendor": {
"user_id": 3245
}
"client": "myclient#email.com"
}
Response:
{
"name": "My Project"
"description": "My Project'description"
"vendor": {
"user_id": 3245,
"name": "John Doe"
}
"client": {
"user_id": 2754,
"name": "Peter Doe"
}
"creator": {
"user_id": 3245,
"name": "John Doe"
}
}
So we can see that the type of data exchanged in the creation of a project differs.
Can I link directly to my model application (after validation of course)?
How should I go about updating a project?
Is it better to create Transfer Objects and managed into my business logic and a TransfertObject for input and one for output?