示例#1
0
    def form_valid(self, form):
        """
        Display a message upon successful tour creation.
        """
        response = super(ServerPollRateUpdate, self).form_valid(form)
        messages.success(self.request, 'Server Poll Rate and Range Updated Successfully.')

        set_server_polling_rate()

        return response
示例#2
0
    def form_valid(self, form):
        """
        Display a message upon successful tour creation.
        """
        response = super(TourConfigAdd, self).form_valid(form)
        messages.success(self.request, 'Tour created successfully.')

        set_server_polling_rate()

        return response
示例#3
0
    def form_valid(self, form):
        """
        Override form_valid functionality to send message via GCM to Android
        devices with new TourConfig
        """
        self.object = form.save(commit=False)
        push_ids = self.get_push_ids()
        headers = {}
        headers[settings.GCM_API_KEY_HEADER] = 'key='+settings.GCM_API_KEY
        headers['Content-Type'] = 'application/json'
        data = {}
        data['registration_ids'] = push_ids

        # Hack-y fix to get just the fields from the TourConfig instance
        newconfig = json.loads(serializers.serialize('json', [self.object]))[0]['fields']
        # Multiple the unix times stored in the database by 1000 because Android
        # uses milliseconds
        newconfig['start_time'] = newconfig['start_time']*1000
        newconfig['max_tour_time'] = newconfig['max_tour_time']*1000
        newconfig['max_tour_time'] = newconfig['max_tour_time'] - newconfig['start_time']
        data['data'] = {}
        data['data'][settings.JSON_KEYS['TOUR_CONFIG']] = newconfig
        # Comment out the following line to actually send messages
        #data['dry_run'] = True

        req = Request(settings.GCM_SEND_URL, json.dumps(data), headers)
        if len(push_ids) > 0:
            logger.debug('Sending request to GCM server:\n%s' % req.get_data())
            resp = urlopen(req)
            # TODO
            # Display a message on failure to send GCM message
        else:
            logger.debug('No push_ids registered; not sending updated config.')

        self.object = form.save()
        messages.success(self.request, 'Tour updated successfully.')

        set_server_polling_rate()

        return HttpResponseRedirect(self.get_success_url())