Skip to content

Commit cee391a

Browse files
committed
API
1 parent 7bfea67 commit cee391a

File tree

11 files changed

+84
-13
lines changed

11 files changed

+84
-13
lines changed

.idea/Graphs.iml

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DjangoGraphs/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __str__(self):
4141

4242
class Graph(models.Model):
4343
name = models.CharField(max_length=64, default='')
44-
title = models.CharField(max_length=64, default='')
44+
title = models.CharField(max_length=64, default='', blank=True)
4545
dashboard = models.BooleanField(default=False)
4646
public = models.BooleanField(default=False)
4747
selector = models.ManyToManyField(GraphSelector)

DjangoGraphs/serializers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django.utils.translation import gettext as _
2+
from rest_framework import serializers
3+
4+
from DjangoGraphs.models import DataEntry
5+
6+
7+
class DataEntrySerializer(serializers.ModelSerializer):
8+
view_name = 'api_entry'
9+
10+
class Meta:
11+
model = DataEntry
12+
fields = ('type', 'instance', 'value', 'timestamp')
13+
extra_kwargs = {
14+
'url': {'view_name': 'api_entry'}
15+
}

DjangoGraphs/tables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class GraphTable(tables.Table):
1212
options = tables.TemplateColumn(
1313
"<a href='{% url 'delete_graph' record.id %}' class='btn btn-danger'><i class='fas fa-trash'></i></a> " +
1414
"<a href='{% url 'view_graph' record.id %}' class='btn btn-success'><i class='fas fa-eye'></i></a> " +
15-
"<a href='{% url 'view_graph_advanced' record.id %}' class='btn btn-info'><i class='fas fa-eye'></i></a>", #TODO icon
15+
"<a href='{% url 'view_graph_advanced' record.id %}' class='btn btn-info'><i class='fas fa-binoculars'></i></a>",
1616
verbose_name=_('Options'))
1717

1818
class Meta:

DjangoGraphs/templates/advanced_graph_view.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,18 @@
7171
aggregate_rollover: true,
7272
mouseover: function (d, i) {
7373
var pf = d3.format('.0s');
74-
//debugger;
74+
7575
d3.select('#graph svg .mg-active-datapoint').style('fill', 'white')
76-
.text('Header').attr("x", 0).attr("dy","1.2em");
77-
d3.select('#graph svg .mg-active-datapoint').append("tspan").attr("dy", "1em").attr("x", "0").style('fill', 'red')
78-
.text('Test 2');
79-
d3.select('#graph svg .mg-active-datapoint').append("tspan").attr("dy", "1em").attr("x", "0").style('fill', 'red')
80-
.text('Test 2');
76+
.text(moment(d.key).format('hh:mm, DD. MMM YYYY')).attr("x", 0).attr("dy","1.2em");
77+
78+
for (x in d.values) {
79+
let line = d.values[x];
80+
d3.select('#graph svg .mg-active-datapoint').append("tspan").style('fill', colors[(line.index - 1)])
81+
.text( legend[(line.index - 1)] ).attr("x", 0).attr("dy","1.2em");
82+
83+
d3.select('#graph svg .mg-active-datapoint').append("tspan").style('fill', 'white')
84+
.text( ' - ' + line.value + ' ' + units[(line.index - 1)]);
85+
}
8186
}
8287
};
8388

DjangoGraphs/templates/base.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@
6363
class="fas fa-inbox"></i> {% trans 'Instances' %}</a>
6464
</div>
6565
</li>
66+
<li class="nav-item dropdown">
67+
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown"
68+
aria-haspopup="true" aria-expanded="false">
69+
<i class="fas fa-exchange-alt"></i> {% trans 'API' %}
70+
</a>
71+
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
72+
<a class="dropdown-item" href="{% url 'api-root' %}" target="_blank"><i
73+
class="fas fa-laptop-code"></i> {% trans 'API Browser' %}</a>
74+
<a class="dropdown-item" href="{% url 'api-docs:docs-index' %}" target="_blank"><i
75+
class="fas fa-book"></i> {% trans 'API Docs' %}</a>
76+
77+
</div>
78+
</li>
79+
80+
81+
6682
{% endif %}
6783
</ul>
6884

DjangoGraphs/urls.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
from django.urls import path
1+
from django.db import router
2+
from django.urls import path, include
3+
from rest_framework import routers
4+
from rest_framework.documentation import include_docs_urls
25

36
from .views import *
47

8+
router = routers.DefaultRouter()
9+
router.register(r'entry', api.DataEntrySet)
10+
511
urlpatterns = [
612

713
path('', views.index, name='index'),
@@ -30,4 +36,10 @@
3036
path('delete/instance/<int:pk>/', edit.InstanceDelete.as_view(), name='delete_instance'),
3137

3238
path('redirect/delete/<slug:name>/<int:pk>/', edit.delete_redirect, name='redirect_delete'),
39+
40+
# API
41+
path('api/docs', include_docs_urls(title='DjangoGraphs API', public=False), name='api_docs'),
42+
path('api/auth/', include('rest_framework.urls')),
43+
44+
path('api/', include(router.urls)),
3345
]

DjangoGraphs/views/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
from .edit import *
33
from .new import *
44
from .lists import *
5+
from .api import *

DjangoGraphs/views/api.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from rest_framework import permissions, viewsets
2+
from rest_framework.response import Response
3+
from rest_framework.views import APIView
4+
5+
from django.utils.translation import gettext as _
6+
7+
from ..serializers import *
8+
9+
10+
class DataEntrySet(viewsets.ModelViewSet):
11+
"""
12+
API endpoint that allows data entries to be viewed or edited.
13+
"""
14+
queryset = DataEntry.objects.all()
15+
serializer_class = DataEntrySerializer
16+
17+
permission_classes = (permissions.IsAuthenticated,)

Graphs/settings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
'django.contrib.staticfiles',
3737
'django_tables2',
3838
'crispy_forms',
39+
'rest_framework',
3940
'DjangoGraphs.apps.DjangographsConfig',
4041
]
4142

@@ -70,7 +71,6 @@
7071

7172
WSGI_APPLICATION = 'Graphs.wsgi.application'
7273

73-
7474
# Database
7575
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
7676

@@ -103,7 +103,6 @@
103103
},
104104
]
105105

106-
107106
# Internationalization
108107
# https://docs.djangoproject.com/en/2.2/topics/i18n/
109108

0 commit comments

Comments
 (0)