def form_valid(self, form):
        """
        save the data
        :param form:
        :return:
        """
        valid = True
        # 'name' is injected in the clean() of the form line 56
        name = form.cleaned_data.get('name').name
        user = self.request.user
        form.save(user=user, service_name=name)

        sa = ServicesActivated.objects.get(name=name)
        if sa.auth_required and sa.self_hosted:
            # trigger the checking of the service
            from django_th.services import default_provider
            default_provider.load_services()
            service_provider = default_provider.get_service(name)
            result = service_provider.check(self.request, user)
            if result is not True:
                # the call of the API failed due to an error which is in the result string
                # return by the call of the API
                form.add_error('host', result)
                messages.error(self.request, result)
                return redirect('edit_service', pk=self.kwargs.get(self.pk_url_kwarg))

        if valid:
            messages.success(self.request, _('Service %s modified successfully') % name.split('Service')[1])
            return HttpResponseRedirect(reverse('user_services'))
Exemple #2
0
    def reading(self, service):
        """
           get the data from the service and put theme in cache
           :param service: service object to read
           :type service: object
        """
        now = arrow.utcnow().to(settings.TIME_ZONE).format(
            'YYYY-MM-DD HH:mm:ssZZ')
        # flag to know if we have to update
        to_update = False

        # counting the new data to store to display them in the log
        # provider - the service that offer data
        provider_token = service.provider.token
        default_provider.load_services()
        service_provider = default_provider.get_service(
            str(service.provider.name.name))
        # check if the service has already been triggered
        # if date_triggered is None, then it's the first run
        # so it will be set to "now"
        date_triggered = service.date_triggered if service.date_triggered \
            else now
        # 1) get the data from the provider service
        # get a timestamp of the last triggered of the service
        kwargs = {'token': provider_token,
                  'trigger_id': service.id,
                  'date_triggered': date_triggered}
        data = self.provider(service_provider, **kwargs)
        # counting the new data to store to display them in the log
        count_new_data = len(data) if data else 0
        if count_new_data > 0:
            to_update = True

        if to_update:
            logger.info("{} - {} new data".format(service, count_new_data))
Exemple #3
0
    def form_valid(self, form):
        name = form.cleaned_data.get('name').name
        user = self.request.user
        if UserService.objects.filter(name=name, user=user).exists():
            from django.contrib import messages
            messages.warning(
                self.request,
                'Service %s already activated' % name.split('Service')[1])
            return HttpResponseRedirect(reverse('user_services'))
        else:
            form.save(user=user)

            sa = ServicesActivated.objects.get(
                name=form.cleaned_data.get('name').name)
            # let's build the 'call' of the auth method
            # which own to a Service Class
            if sa.auth_required:
                # use the default_provider to get the object from the ServiceXXX
                default_provider.load_services()
                service_object = default_provider.get_service(
                    str(form.cleaned_data.get('name').name))
                # get the class object
                lets_auth = getattr(service_object, 'auth')
                # call the auth func from this class
                # and redirect to the external service page
                # to auth the application django-th to access to the user
                # account details
                return redirect(lets_auth(self.request))

            return HttpResponseRedirect(
                reverse('user_services', args=['added']))
Exemple #4
0
    def form_valid(self, form):
        name = form.cleaned_data.get('name').name
        user = self.request.user
        if UserService.objects.filter(name=name, user=user).exists():
            from django.contrib import messages
            messages.warning(self.request, 'Service %s already activated' %
                             name.split('Service')[1])
            return HttpResponseRedirect(reverse('user_services'))
        else:
            form.save(user=user)

            sa = ServicesActivated.objects.get(
                name=form.cleaned_data.get('name').name)
            # let's build the 'call' of the auth method
            # which own to a Service Class
            if sa.auth_required:
                # use the default_provider to get the object from the ServiceXXX
                default_provider.load_services()
                service_object = default_provider.get_service(
                    str(form.cleaned_data.get('name').name))
                # get the class object
                lets_auth = getattr(service_object, 'auth')
                # call the auth func from this class
                # and redirect to the external service page
                # to auth the application django-th to access to the user
                # account details
                return redirect(lets_auth(self.request))

            return HttpResponseRedirect(reverse('user_services',
                                                args=['added']))
Exemple #5
0
    def reading(self, service):
        """
           get the data from the service and put theme in cache
           :param service: service object to read
           :type service: object
        """
        now = arrow.utcnow().to(
            settings.TIME_ZONE).format('YYYY-MM-DD HH:mm:ssZZ')
        # counting the new data to store to display them in the log
        # provider - the service that offer data
        provider_token = service.provider.token
        default_provider.load_services()
        service_provider = default_provider.get_service(
            str(service.provider.name.name))
        # check if the service has already been triggered
        # if date_triggered is None, then it's the first run
        # so it will be set to "now"
        date_triggered = service.date_triggered if service.date_triggered \
            else now
        # 1) get the data from the provider service
        # get a timestamp of the last triggered of the service
        kwargs = {
            'token': provider_token,
            'trigger_id': service.id,
            'date_triggered': date_triggered
        }
        data = self.provider(service_provider, **kwargs)

        if len(data) > 0:
            logger.info("{} - {} new data".format(service, len(data)))
        elif data is False:
            # if data is False, something went wrong
            self.is_ceil_reached(service)
Exemple #6
0
    def reading(self, service):
        """
           get the data from the service and put theme in cache
           :param service: service object to read
           :type service: object
        """
        # counting the new data to store to display them in the log provider - the service that offer data
        provider_token = service.provider.token
        default_provider.load_services()
        service_provider = default_provider.get_service(
            str(service.provider.name.name))
        date_triggered = service.date_triggered if service.date_triggered else service.date_created

        # get the data from the provider service
        kwargs = {
            'token': provider_token,
            'trigger_id': service.id,
            'date_triggered': date_triggered
        }
        data = self.provider(service_provider, **kwargs)

        if len(data) > 0:
            logger.info("{} - {} new data".format(service, len(data)))
        elif data is False:
            # if data is False, something went wrong
            self.is_ceil_reached(service)
def renew_service(request, pk):
    """
        renew an existing service
        :param pk: the primary key of the service to renew
        :type pk: int
    """
    default_provider.load_services()
    service = get_object_or_404(ServicesActivated, pk=pk)
    service_name = str(service.name)
    service_object = default_provider.get_service(service_name)
    lets_auth = getattr(service_object, 'auth')
    return redirect(lets_auth(request))
def finalcallback(request, **kwargs):
    """
        let's do the callback of the related service after
        the auth request from UserServiceCreateView
    """
    default_provider.load_services()
    service_name = kwargs.get('service_name')
    service_object = default_provider.get_service(service_name)
    lets_callback = getattr(service_object, 'callback')
    # call the auth func from this class
    # and redirect to the external service page
    # to auth the application django-th to access to the user
    # account details
    return render_to_response(lets_callback(request))
def finalcallback(request, **kwargs):
    """
        let's do the callback of the related service after
        the auth request from UserServiceCreateView
    """
    default_provider.load_services()
    service_name = kwargs.get('service_name')
    service_object = default_provider.get_service(service_name)
    lets_callback = getattr(service_object, 'callback')
    # call the auth func from this class
    # and redirect to the external service page
    # to auth the application django-th to access to the user
    # account details
    return render_to_response(lets_callback(request))
    def handle(self, *args, **options):
        """
            run the main process
        """
        default_provider.load_services()
        trigger = TriggerService.objects.filter(status=True).select_related(
            'consumer__name', 'provider__name')
        if trigger:
            for service in trigger:

                # provider - the service that offer data
                service_name = str(service.provider.name.name)
                service_provider = default_provider.get_service(service_name)

                # consumer - the service which uses the data
                service_name = str(service.consumer.name.name)
                service_consumer = default_provider.get_service(service_name)

                # First run
                if service.date_triggered is None:
                    logger.debug("first run for %s => %s " % (str(
                        service.provider.name), str(service.consumer.name.name)))

                    asyncio.get_event_loop().run_until_complete(
                        self.my_dummy_provider())

                # another run
                else:
                    asyncio.get_event_loop().run_until_complete(
                        self.my_provider(service_provider,
                                         service.provider.token,
                                         service.id,
                                         service.date_triggered))
                    asyncio.get_event_loop().run_until_complete(
                        self.my_consumer(service_consumer,
                                         service.consumer.token,
                                         service.id,
                                         service.date_triggered))

                asyncio.get_event_loop().run_until_complete(
                    self.update_trigger(service))
                asyncio.get_event_loop().run_forever()

        else:
            print("No trigger set by any user")
Exemple #11
0
    def handle(self, *args, **options):
        """
            run the main process
        """
        default_provider.load_services()
        trigger = TriggerService.objects.filter(status=True).select_related(
            'consummer__name', 'provider__name')
        if trigger:
            for service in trigger:

                # provider - the service that offer datas
                service_name = str(service.provider.name.name)
                service_provider = default_provider.get_service(service_name)

                # consumer - the service which uses the data
                service_name = str(service.consumer.name.name)
                service_consumer = default_provider.get_service(service_name)

                # First run
                if service.date_triggered is None:
                    logger.debug("first run for %s => %s " %
                                 (str(service.provider.name),
                                  str(service.consumer.name.name)))

                    asyncio.get_event_loop().run_until_complete(
                        self.my_dummy_provider())

                # another run
                else:
                    asyncio.get_event_loop().run_until_complete(
                        self.my_provider(service_provider,
                                         service.provider.token, service.id,
                                         service.date_triggered))
                    asyncio.get_event_loop().run_until_complete(
                        self.my_consumer(service_consumer,
                                         service.consumer.token, service.id,
                                         service.date_triggered))

                asyncio.get_event_loop().run_until_complete(
                    self.update_trigger(service))
                asyncio.get_event_loop().run_forever()

        else:
            print("No trigger set by any user")
    def form_valid(self, form):
        default_provider.load_services()
        self.object = form.save(user=self.request.user)

        sa = ServicesActivated.objects.get(name=form.cleaned_data['name'].name)
        # let's build the 'call' of the auth method which own to a Service Class
        if sa.auth_required:
            # use the default_provider to get the object from the ServiceXXX
            service_object = default_provider.get_service(
                str(form.cleaned_data['name'].name))
            # get the class object
            lets_auth = getattr(service_object, 'auth')
            # call the auth func from this class
            # and redirect to the external service page
            # to auth the application django-th to access to the user
            # account details
            return redirect(lets_auth(self.request))

        return HttpResponseRedirect(reverse('service_add_thanks'))
Exemple #13
0
def save_data(trigger_id, data):
    """
        call the consumer and handle the data
        :param trigger_id:
        :param data:
        :return:
    """
    status = True
    # consumer - the service which uses the data
    default_provider.load_services()
    service = TriggerService.objects.get(id=trigger_id)

    service_consumer = default_provider.get_service(str(service.consumer.name.name))
    kwargs = {'user': service.user}

    if len(data) > 0:
        getattr(service_consumer, '__init__')(service.consumer.token, **kwargs)
        status = getattr(service_consumer, 'save_data')(service.id, **data)

    return status
    def form_valid(self, form):
        name = form.cleaned_data.get('name').name
        user = self.request.user
        form.save(user=user, service_name=self.kwargs.get('service_name'))

        sa = ServicesActivated.objects.get(name=name)
        # let's build the 'call' of the auth method
        # which own to a Service Class
        if sa.auth_required:
            # use the default_provider to get the object from the ServiceXXX
            default_provider.load_services()
            service_object = default_provider.get_service(str(name))
            # get the class object
            lets_auth = getattr(service_object, 'auth')
            # call the auth func from this class
            # and redirect to the external service page
            # to auth the application django-th to access to the user
            # account details
            return redirect(lets_auth(self.request))
        messages.success(self.request, _('Service %s activated successfully') % name.split('Service')[1])
        return HttpResponseRedirect(reverse('user_services'))
Exemple #15
0
    def reading(self, service):
        """
           get the data from the service and put theme in cache
           :param service: service object to read
           :type service: object
        """
        # counting the new data to store to display them in the log provider - the service that offer data
        provider_token = service.provider.token
        default_provider.load_services()
        service_provider = default_provider.get_service(str(service.provider.name.name))
        date_triggered = service.date_triggered if service.date_triggered else service.date_created

        # get the data from the provider service
        kwargs = {'token': provider_token, 'trigger_id': service.id, 'date_triggered': date_triggered}
        data = self.provider(service_provider, **kwargs)

        if len(data) > 0:
            logger.info("{} - {} new data".format(service, len(data)))
        elif data is False:
            # if data is False, something went wrong
            self.is_ceil_reached(service)
Exemple #16
0
def save_data(trigger_id, data):
    """
        call the consumer and handle the data
        :param trigger_id:
        :param data:
        :return:
    """
    status = True
    # consumer - the service which uses the data
    default_provider.load_services()
    service = TriggerService.objects.get(id=trigger_id)

    service_consumer = default_provider.get_service(
        str(service.consumer.name.name))
    kwargs = {'user': service.user}

    if len(data) > 0:
        getattr(service_consumer, '__init__')(service.consumer.token, **kwargs)
        status = getattr(service_consumer, 'save_data')(service.id, **data)

    return status
Exemple #17
0
                count_new_data += 1
        # otherwise do nothing
        else:
            if 'title' in data:
                logger.debug("data outdated skiped : [{}] {}".format(
                    published, data['title']))
            else:
                logger.debug("data outdated skiped : [{}] ".format(published))

    else:
        # return the number of updates ( to be displayed in the log )
        yield from q2.put(count_new_data)


default_provider.load_services()
trigger = TriggerService.objects.filter(status=True)
if trigger:
    for service in trigger:

        # provider - the service that offer datas
        service_name = str(service.provider.name.name)
        service_provider = default_provider.get_service(service_name)

        # consumer - the service which uses the data
        service_name = str(service.consumer.name.name)
        service_consumer = default_provider.get_service(service_name)

        # First run
        if service.date_triggered is None:
            logger.debug(
Exemple #18
0
def main():
    default_provider.load_services()
    # let's go
    go()
Exemple #19
0
import arrow

from celery import shared_task

from django.conf import settings
from django.core.cache import caches
from django_th.services import default_provider
from django_th.models import TriggerService
from django_th.my_services import MyService

from django.utils.log import getLogger
# create logger
logger = getLogger('django_th.trigger_happy')


default_provider.load_services()


def update_trigger(service):
    """
        update the date when occurs the trigger
    """
    now = arrow.utcnow().to(settings.TIME_ZONE).format(
        'YYYY-MM-DD HH:mm:ss')
    TriggerService.objects.filter(id=service.id).update(date_triggered=now)


def to_datetime(data):
    """
        convert Datetime 9-tuple to the date and time format
        feedparser provides this 9-tuple
Exemple #20
0
    def handle(self, *args, **options):
        """
            run the main process
        """
        default_provider.load_services()
        trigger = TriggerService.objects.filter(status=True).select_related('consummer__name', 'provider__name')
        if trigger:
            for service in trigger:
                # flag to know if we have to update
                to_update = False
                # flag to get the status of a service
                status = False
                # counting the new data to store to display them in the log
                count_new_data = 0
                # provider - the service that offer datas
                service_name = str(service.provider.name.name)
                service_provider = default_provider.get_service(service_name)

                # consumer - the service which uses the data
                service_name = str(service.consumer.name.name)
                service_consumer = default_provider.get_service(service_name)

                # check if the service has already been triggered
                # if date_triggered is None, then it's the first run
                if service.date_triggered is None:
                    logger.debug("first run for %s => %s " % (str(
                        service.provider.name),
                        str(service.consumer.name.name)))
                    to_update = True
                    status = True
                # run run run
                else:
                    # 1) get the datas from the provider service
                    # get a timestamp of the last triggered of the service
                    datas = getattr(service_provider, 'process_data')(
                        service.provider.token, service.id,
                        service.date_triggered)
                    consumer = getattr(service_consumer, 'save_data')

                    published = ''
                    which_date = ''

                    # 2) for each one
                    for data in datas:
                        # if in a pool of data once of them does not have
                        # a date, will take the previous date for this one
                        # if it's the first one, set it to 00:00:00

                        # let's try to determine the date contained in
                        # the data...
                        published = self.to_datetime(data)

                        if published is not None:
                            # get the published date of the provider
                            published = arrow.get(str(published), 'YYYY-MM-DD HH:mm:ss').to(settings.TIME_ZONE)
                            # store the date for the next loop
                            # if published became 'None'
                            which_date = published
                        #... otherwise set it to 00:00:00 of the current date
                        if which_date == '':
                            # current date
                            which_date = arrow.utcnow().replace(hour=0, minute=0, second=0).to(settings.TIME_ZONE)
                            published = which_date
                        if published is None and which_date != '':
                            published = which_date
                        # 3) check if the previous trigger is older than the
                        # date of the data we retreived
                        # if yes , process the consumer

                        # add the TIME_ZONE settings
                        # to localize the current date
                        date_triggered = arrow.get(str(service.date_triggered), 'YYYY-MM-DD HH:mm:ss').to(settings.TIME_ZONE)

                        # if the published date if greater or equal to the last
                        # triggered event ... :
                        if date_triggered is not None and \
                           published is not None and \
                           published >= date_triggered:

                            if 'title' in data:
                                logger.info("date {} >= date triggered {} title {}".format(published, date_triggered, data['title']))
                            else:
                                logger.info("date {} >= date triggered {} ".format(published, date_triggered))

                            status = consumer(
                                service.consumer.token, service.id, **data)

                            to_update = True
                            count_new_data += 1
                        # otherwise do nothing
                        else:
                            if 'title' in data:
                                logger.debug("data outdated skiped : [{}] {}".format(published, data['title']))
                            else:
                                logger.debug("data outdated skiped : [{}] ".format(published))

                # update the date of the trigger at the end of the loop
                sentance = "user: {} - provider: {} - consumer: {} - {}"
                if to_update:
                    if status:
                        logger.info((sentance + " - {} new data").format(
                            service.user,
                            service.provider.name.name,
                            service.consumer.name.name,
                            service.description,
                            count_new_data))
                        self.update_trigger(service)
                    else:
                        logger.info((sentance + " AN ERROR OCCURS ").format(
                            service.user,
                            service.provider.name.name,
                            service.consumer.name.name,
                            service.description))
                else:
                    logger.info((sentance + " nothing new").format(
                        service.user,
                        service.provider.name.name,
                        service.consumer.name.name,
                        service.description))
        else:
            self.stdout.write("No trigger set by any user")
Exemple #21
0
def main():
    default_provider.load_services()
    # let's go
    go()