예제 #1
0
    def load_demo_data(self):
        try:
            timeseries = TimeSeries.objects.get(slug=self.demo_timeseries['slug'])
            timeseries.delete()
        except TimeSeries.DoesNotExist:
            pass

        timeseries = TimeSeries(**self.demo_timeseries)
        timeseries.save()

        data_path = os.path.join(os.path.dirname(openorg_timeseries.demo.__file__),
                                 'data', 'example_data.csv')

        readings = []
        with open(data_path, 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                readings.append([dateutil.parser.parse(row[0]),
                                 float(row[1])])
        timeseries.append(readings)
예제 #2
0
    def post(self, request):
        context = self.common(request)
        if not request.user.has_perm('openorg_timeseries.add_timeseries'):
            return self.lacking_privilege('create a new time-series')
        form, archive_formset = context['form'], context['archive_formset']
        if not (form.is_valid() and archive_formset.is_valid()):
            return self.render(request, context, 'timeseries-admin/create')

        time_series = TimeSeries(**self.filtered_dict(form.cleaned_data,
                                                      TimeSeries.common_fields))
        if time_series.is_virtual:
            time_series.equation = form.cleaned_data['equation']
        else:
            config = self.filtered_dict(form.cleaned_data, TimeSeries.config_fields)
            config['archives'] = [f.cleaned_data for f in archive_formset.forms if f.is_valid() and f.cleaned_data]
            time_series.config = config

        try:
            time_series.save()
        except IntegrityError:
            form.errors['slug'] = ErrorList(["A time-series with this slug already exists."])
            return self.get(request, context)
        return HttpResponseSeeOther(time_series.get_admin_url())