Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Allow filtering of objects via the REST API, fixes #43

Roland Geider915faa1bc951
diff --git a/wger/config/models.py b/wger/config/models.py
index d204707a1..ea55fcfc7 100644
--- a/wger/config/models.py
+++ b/wger/config/models.py
@@ -56,8 +56,10 @@ class LanguageConfig(models.Model):
editable=False)
show = models.BooleanField(default=1)
- # Metaclass to set some other properties
class Meta:
+ '''
+ Set some other properties
+ '''
ordering = ["item", "language_target", ]
def __unicode__(self):
diff --git a/wger/exercises/api/resources.py b/wger/exercises/api/resources.py
index 22bba0bae..2926059cd 100644
--- a/wger/exercises/api/resources.py
+++ b/wger/exercises/api/resources.py
@@ -16,6 +16,7 @@
from tastypie import fields
from tastypie.resources import ModelResource
+from tastypie.constants import ALL, ALL_WITH_RELATIONS
from easy_thumbnails.alias import aliases
from easy_thumbnails.files import get_thumbnailer
@@ -42,18 +43,31 @@ class ExerciseResource(ModelResource):
class Meta:
queryset = Exercise.objects.all()
+ filtering = {'id': ALL,
+ "category": ALL_WITH_RELATIONS,
+ "creation_date": ALL,
+ "description": ALL,
+ "images": ALL_WITH_RELATIONS,
+ "language": ALL_WITH_RELATIONS,
+ "muscles": ALL_WITH_RELATIONS,
+ "status": ALL,
+ "name": ALL}
class EquipmentResource(ModelResource):
class Meta:
queryset = Equipment.objects.all()
+ filtering = {'id': ALL,
+ "name": ALL}
class ExerciseCategoryResource(ModelResource):
class Meta:
queryset = ExerciseCategory.objects.all()
+ filtering = {'id': ALL,
+ "name": ALL}
class ExerciseImageResource(ModelResource):
@@ -61,6 +75,11 @@ class ExerciseImageResource(ModelResource):
class Meta:
queryset = ExerciseImage.objects.all()
+ filtering = {'id': ALL,
+ "image": ALL,
+ "is_main": ALL,
+ "license": ALL,
+ "license_author": ALL}
def dehydrate(self, bundle):
'''
@@ -81,13 +100,22 @@ class ExerciseCommentResource(ModelResource):
class Meta:
queryset = ExerciseComment.objects.all()
+ filtering = {'id': ALL,
+ "comment": ALL,
+ "exercise": ALL_WITH_RELATIONS}
class MuscleResource(ModelResource):
class Meta:
queryset = Muscle.objects.all()
+ filtering = {'id': ALL,
+ "name": ALL,
+ "is_front": ALL}
class LanguageResource(ModelResource):
class Meta:
queryset = Language.objects.all()
+ filtering = {'id': ALL,
+ "full_name": ALL,
+ "short_name": ALL}
diff --git a/wger/manager/api/resources.py b/wger/manager/api/resources.py
index d34654f76..77e5daefc 100644
--- a/wger/manager/api/resources.py
+++ b/wger/manager/api/resources.py
@@ -17,6 +17,7 @@
from tastypie import fields
from tastypie.authentication import ApiKeyAuthentication
from tastypie.resources import ModelResource
+from tastypie.constants import ALL, ALL_WITH_RELATIONS
from wger.exercises.api.resources import ExerciseResource
from wger.utils.resources import UserObjectsOnlyAuthorization
@@ -73,6 +74,9 @@ class WorkoutResource(ModelResource):
queryset = Workout.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ "comment": ALL,
+ "creation_date": ALL}
class ScheduleStepResource(ModelResource):
@@ -93,6 +97,9 @@ class ScheduleStepResource(ModelResource):
queryset = ScheduleStep.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ 'schedule': ALL_WITH_RELATIONS,
+ 'workout': ALL_WITH_RELATIONS}
class ScheduleResource(ModelResource):
@@ -112,6 +119,10 @@ class ScheduleResource(ModelResource):
queryset = Schedule.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ 'is_active': ALL,
+ 'is_loop': ALL,
+ 'name': ALL}
class DaysOfWeekResource(ModelResource):
@@ -121,6 +132,8 @@ class DaysOfWeekResource(ModelResource):
class Meta:
queryset = DaysOfWeek.objects.all()
+ filtering = {'id': ALL,
+ 'day_of_week': ALL}
class DayResource(ModelResource):
@@ -141,6 +154,9 @@ class DayResource(ModelResource):
queryset = Day.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ 'description': ALL,
+ 'workout': ALL_WITH_RELATIONS}
class SetResource(ModelResource):
@@ -161,6 +177,10 @@ class SetResource(ModelResource):
queryset = Set.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ 'day': ALL_WITH_RELATIONS,
+ 'order': ALL,
+ 'sets': ALL}
class SettingResource(ModelResource):
@@ -181,6 +201,11 @@ class SettingResource(ModelResource):
queryset = Setting.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ 'exercise': ALL_WITH_RELATIONS,
+ 'order': ALL,
+ 'reps': ALL,
+ 'set': ALL_WITH_RELATIONS}
class WorkoutLogResource(ModelResource):
@@ -201,3 +226,9 @@ class WorkoutLogResource(ModelResource):
queryset = WorkoutLog.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ 'date': ALL,
+ 'exercise': ALL_WITH_RELATIONS,
+ 'reps': ALL,
+ 'weight': ALL,
+ 'workout': ALL_WITH_RELATIONS}
diff --git a/wger/manager/templates/user/api_key.html b/wger/manager/templates/user/api_key.html
index bafbe8909..a2d1c866f 100644
--- a/wger/manager/templates/user/api_key.html
+++ b/wger/manager/templates/user/api_key.html
@@ -16,10 +16,10 @@ the header or in the request as GET parameters. For example:{% endblocktrans %}
<pre>
# In header
-Authorization: ApiKey myusername:1234567890abcde...
+Authorization: ApiKey {{user.username}}:{% if key %}{{key.key|truncatechars:15}}{% else %}1234567890abcde...{% endif %}
# In GET parameter
-https://wger.de/api/v1/workout/?username=myusername&api_key=1234567890abcde...
+https://wger.de/api/v1/workout/?username={{user.username}}&api_key={% if key %}{{key.key|truncatechars:15}}{% else %}1234567890abcde...{% endif %}
</pre>
</p>
diff --git a/wger/nutrition/api/resources.py b/wger/nutrition/api/resources.py
index ff76349c8..7625a7685 100644
--- a/wger/nutrition/api/resources.py
+++ b/wger/nutrition/api/resources.py
@@ -17,6 +17,7 @@
from tastypie import fields
from tastypie.authentication import ApiKeyAuthentication
from tastypie.resources import ModelResource
+from tastypie.constants import ALL, ALL_WITH_RELATIONS
from wger.nutrition.models import Ingredient
from wger.nutrition.models import WeightUnit
@@ -30,11 +31,26 @@ from wger.utils.resources import UserObjectsOnlyAuthorization
class IngredientResource(ModelResource):
class Meta:
queryset = Ingredient.objects.all()
+ filtering = {'id': ALL,
+ 'carbohydrates': ALL,
+ 'carbohydrates_sugar': ALL,
+ 'creation_date': ALL,
+ 'energy': ALL,
+ 'fat': ALL,
+ 'fat_saturated': ALL,
+ 'fibres': ALL,
+ 'name': ALL,
+ 'protein': ALL,
+ 'sodium': ALL,
+ 'status': ALL,
+ 'update_date': ALL}
class WeightUnitResource(ModelResource):
class Meta:
queryset = WeightUnit.objects.all()
+ filtering = {'id': ALL,
+ 'name': ALL}
class IngredientToWeightUnit(ModelResource):
@@ -44,6 +60,11 @@ class IngredientToWeightUnit(ModelResource):
class Meta:
queryset = IngredientWeightUnit.objects.all()
+ filtering = {'id': ALL,
+ 'amount': ALL,
+ 'gramm': ALL,
+ 'ingredient': ALL_WITH_RELATIONS,
+ 'unit': ALL_WITH_RELATIONS}
class NutritionPlanResource(ModelResource):
@@ -70,6 +91,10 @@ class NutritionPlanResource(ModelResource):
queryset = NutritionPlan.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ 'creation_date': ALL,
+ 'description': ALL,
+ 'has_goal_calories': ALL}
class MealResource(ModelResource):
@@ -97,6 +122,10 @@ class MealResource(ModelResource):
queryset = Meal.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ 'order': ALL,
+ 'plan': ALL_WITH_RELATIONS,
+ 'time': ALL}
class MealItemResource(ModelResource):
@@ -125,3 +154,9 @@ class MealItemResource(ModelResource):
queryset = MealItem.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ 'amount': ALL,
+ 'ingredient': ALL_WITH_RELATIONS,
+ 'meal': ALL_WITH_RELATIONS,
+ 'order': ALL,
+ 'weight_unit': ALL_WITH_RELATIONS}
diff --git a/wger/software/templates/api.html b/wger/software/templates/api.html
index 29e452c33..a44921c0b 100644
--- a/wger/software/templates/api.html
+++ b/wger/software/templates/api.html
@@ -79,7 +79,7 @@ curl https://wger.de/api/v1/ | python -m json.tool
</code>
<p>This lists out all the different resources available. Each one is listed by
-the resource_name and provides the list_endpoint and the schema for the
+the "resource_name" and provides the "list_endpoint" and the schema for the
resource. Note that these links try to direct you to other parts of the API,
to make exploration/discovery easier.</p>
@@ -93,27 +93,53 @@ curl https://wger.de/api/v1/exercise/schema/ | python -m json.tool
<ul>
<li>By default, you get a paginated set of objects</li>
- <li>In the meta, you get a previous and next. If available, these are URIs to the previous and next pages.</li>
- <li>You get a list of resources/objects under the objects key.</li>
- <li>Each resources/object has a resource_uri field that points to the detail view for that object.</li>
+ <li>In "meta", you get a "previous" and "next". If available, these are URIs to the previous and next pages.</li>
+ <li>?limit=0 returns all resources, without pagination.</li>
+ <li>You get a list of resources/objects under the "objects" key.</li>
+ <li>Each resources/object has a "resource_uri" field that points to the detail view for that object.</li>
<li>Foreign keys are represented as URIs.</li>
</ul>
-<code>
-curl https://wger.de/api/v1/exercise/1/ | python -m json.tool
-</code>
-<p>This simply returns the exercise resource with the ID 1.</p>
+<p>You can also filter the resources, the syntax is the same as the Django ORM.
+Take a look at the <a href="https://docs.djangoproject.com/en/1.6/ref/models/querysets/#field-lookups">field
+lookup documentation</a> for more details on how to use the individual lookups.
+The following field lookups are supported, but don't always make sense
+for all fields (in that case you'll just get an emtpy result). Alternatively you
+can just specify the IDs you want or provide an offset.</p>
+
+<ul>
+ <li>exact/iexact</li>
+ <li>contains/icontains</li>
+ <li>in</li>
+ <li>gt/gte</li>
+ <li>lt/lte</li>
+ <li>startswith/istartswith</li>
+ <li>endswith/iendswith</li>
+ <li>range</li>
+ <li>isnull</li>
+ <li>year</li>
+ <li>month</li>
+ <li>day</li>
+ <li>week_day</li>
+ <li>hour</li>
+ <li>minute</li>
+ <li>second</li>
+</ul>
+
+<p>Some examples:</p>
<code>
-curl https://wger.de/api/v1/foobar/1/?bar=baz | python -m json.tool
+curl https://wger.de/api/v1/exercise/?name__startswith=bicep | python -m json.tool
+curl https://wger.de/api/v1/exercise/?status__in=2,4,5 | python -m json.tool
+curl https://wger.de/api/v1/exercise/?language=1 | python -m json.tool
+curl https://wger.de/api/v1/exercise/?category__name__startswith=ba | python -m json.tool
curl https://wger.de/api/v1/exercise/set/1;3/ | python -m json.tool
curl https://wger.de/api/v1/exercise/?offset=20&limit=20 | python -m json.tool
+curl https://wger.de/api/v1/exercise/1/ | python -m json.tool
</code>
-<p>You can filter the resources, assuming that bar is an
-attribute of foobar. Alternatively you can just specify the IDs you need or
-provide an offset.</p>
+<p></p>
{% endblock %}
diff --git a/wger/weight/api/resources.py b/wger/weight/api/resources.py
index 9a3e85eef..da8af9169 100644
--- a/wger/weight/api/resources.py
+++ b/wger/weight/api/resources.py
@@ -16,6 +16,7 @@
from tastypie.authentication import ApiKeyAuthentication
from tastypie.resources import ModelResource
+from tastypie.constants import ALL
from wger.weight.models import WeightEntry
from wger.utils.resources import UserObjectsOnlyAuthorization
@@ -38,3 +39,6 @@ class WeightEntryResource(ModelResource):
queryset = WeightEntry.objects.all()
authentication = ApiKeyAuthentication()
authorization = UserObjectsOnlyAuthorization()
+ filtering = {'id': ALL,
+ 'creation_date': ALL,
+ 'weight': ALL}