Example #1
0
 def remind(self):
     for lease in ResourceLease.objects.filter(expires <= datetime.date.today()):
         #TODO : set reminder count and adjust expires
         lease.reminder += 1;
         lease.expires = self.updateExpires(lease)
         lease.save()
         users = []
         for key in ResourceSubscribers.filter(resource_id == lease.resource.id):
             if key.subscriber.email not in users:
                users.append(key.subscriber.email)
         for user in users:
             mailer.mail(owner=user, resource=lease.resource)
         if lease.resource.callbackurl:
             import httplib2
             import urllib
             import base64
             import json
             try:
                url = lease.resource.callbackurl
                auth = lease.resource.callbackauth
                logger.info('url='+url)
                params = urllib.urlencode({'id':lease.resource.id, 'msg': 'lease expiration reminder',
                      'subscribers': users, 'count':lease.reminder, 'next':repr(lease.expires)})
                response, content = http.request(url, 'POST',
                    headers={"Content-Type": "application/json", 'Authorization' : 'Basic ' + auth})
                if response.status != 200:
                    logger.error('status='+str(response.status))
                logger.info(repr(content))
                msg = 'callback invoked'
                logger.info(msg)
             except:
                pass 
     pass
Example #2
0
  def create(self, request):
    term = ''
    guid = None
    email = ''
    leaseid = ''
    try:
        if request.POST.get('term') and request.POST['term']:
           term = request.POST['term'] 
        if request.POST.get('email') and request.POST['email']:
           email = request.POST['email']
        if request.POST.get('guid') and request.POST['guid']:
           guid = request.POST['guid']
        if request.POST.get('leaseid') and request.POST['leaseid']:
           leaseid = request.POST['leaseid']
        if term==None or email==None or int(term) < 0 or int(term) > 365:
            return HttpResponse(json.dumps({'status':'error', 
                   'msg':'Term and email required.Term has to be less than 365 days'}), status=400)
        if guid == None:
              resource = Resource()
              #guid = uuid.uuid4
              #resource.guid = guid
              resource.callbackurl = ''
              resource.callbackauth = ''
              resource.save()
              print('resource saved')
              guid = resource.guid
        try:
              print('all_resources='+repr(Resource.objects.all()))
              resource = Resource.objects.get(guid=guid)
        except Resource.DoesNotExist:
              resource = None
              return HttpResponse(json.dumps({
               'status': 'error',
               'msg': 'Resource could not be created',
               'lease':'',
               }), status=500)
        try:
            subscriber = Subscriber.objects.get(email=email)
        except Subscriber.DoesNotExist:
            subscriber = None
            pass
        if subscriber == None:
           subscriber = Subscriber()
           subscriber.email = email
           subscriber.save()
        subscriber = Subscriber.objects.get(email=email)

        if not leaseid:
            lease = ResourceLease()
            print('lease.resource='+repr(resource))
            lease.resource = resource
            lease.subscriber = subscriber
            lease.term = int(term)
            from datetime import timedelta
            lease.expires = datetime.datetime.now() + timedelta(days=int(term))
            lease.status = 'A'
            lease.save()
            print('lease.id'+str(lease.id))
            leaseid = str(lease.id)
        try:
            lease = ResourceLease.objects.get(id=leaseid)
        except ResourceLease.DoesNotExist:
            lease = None
            return HttpResponse(json.dumps({
               'status': 'error',
               'msg': 'Resource could not be created',
               'lease':'',
               }), status=500)
        queryset = ResourceSubscribers.objects.filter(resource=resource)
        recipients = []
        for item in queryset:
            recipients.append(item.subscriber.email)
        if email not in recipients:
            recipients.append(email)
            ressub = ResourceSubscribers()
            ressub.resource = resource
            ressub.subscriber = subscriber
            ressub.save()
        return HttpResponse(json.dumps({'status':'success', 'guid':str(guid), 'leaseid':str(leaseid), 'subscribers':recipients}))
    except:
        import sys
        exc_type, exc_value, exc_traceback = sys.exc_info()
        import traceback
        msg = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
        logger.warn(msg)
        return HttpResponse(json.dumps({
               'status': 'error',
               'msg': msg,
               'lease':'',
               }), status=500)
    finally:
        pass