Exemple #1
0
def myvessels(request,
              get_form=False,
              action_summary="",
              action_detail="",
              remove_summary=""):
    try:
        user = _validate_and_get_geniuser(request)
    except LoggedInButFailedGetGeniUserError:
        return _show_failed_get_geniuser_page(request)

    # get_form of None means don't show the form to acquire vessels.
    if interface.get_available_vessel_credits(user) == 0:
        get_form = None
    elif get_form is False:
        get_form = forms.gen_get_form(user)

    # shared vessels that are used by others but which belong to this user (TODO)
    shvessels = []

    # this user's used vessels
    my_vessels_raw = interface.get_acquired_vessels(user)
    my_vessels = interface.get_vessel_infodict_list(my_vessels_raw)

    # this user's number of donations, max vessels, total vessels and free credits
    my_donations = interface.get_donations(user)
    my_max_vessels = interface.get_available_vessel_credits(user)
    my_free_vessel_credits = interface.get_free_vessel_credits_amount(user)
    my_total_vessel_credits = interface.get_total_vessel_credits(user)

    for vessel in my_vessels:
        if vessel["expires_in_seconds"] <= 0:
            # We shouldn't ever get here, but just in case, let's handle it.
            vessel["expires_in"] = "Expired"
        else:
            days = vessel["expires_in_seconds"] / (3600 * 24)
            hours = vessel["expires_in_seconds"] / 3600 % 24
            minutes = vessel["expires_in_seconds"] / 60 % 60
            vessel["expires_in"] = "%dd %dh %dm" % (days, hours, minutes)

    # return the used resources page constructed from a template
    return render_to_response('control/myvessels.html', {
        'username': user.username,
        'num_vessels': len(my_vessels),
        'my_vessels': my_vessels,
        'sh_vessels': shvessels,
        'get_form': get_form,
        'action_summary': action_summary,
        'action_detail': action_detail,
        'my_donations': len(my_donations),
        'my_max_vessels': my_max_vessels,
        'free_vessel_credits': my_free_vessel_credits,
        'total_vessel_credits': my_total_vessel_credits,
        'remove_summary': remove_summary
    },
                              context_instance=RequestContext(request))
Exemple #2
0
def myvessels(request, get_form=False, action_summary="", action_detail="", remove_summary=""):
  try:
    user = _validate_and_get_geniuser(request)
  except LoggedInButFailedGetGeniUserError:
    return _show_failed_get_geniuser_page(request)
  
  # get_form of None means don't show the form to acquire vessels.
  if interface.get_available_vessel_credits(user) == 0:
    get_form = None
  elif get_form is False:
    get_form = forms.gen_get_form(user)

  # shared vessels that are used by others but which belong to this user (TODO)
  shvessels = []

  # this user's used vessels
  my_vessels_raw = interface.get_acquired_vessels(user)
  my_vessels = interface.get_vessel_infodict_list(my_vessels_raw)
  
  # this user's number of donations, max vessels, total vessels and free credits
  my_donations = interface.get_donations(user)
  my_max_vessels = interface.get_available_vessel_credits(user)	
  my_free_vessel_credits = interface.get_free_vessel_credits_amount(user)
  my_total_vessel_credits = interface.get_total_vessel_credits(user)

  for vessel in my_vessels:
    if vessel["expires_in_seconds"] <= 0:
      # We shouldn't ever get here, but just in case, let's handle it.
      vessel["expires_in"] = "Expired"
    else:
      days = vessel["expires_in_seconds"] / (3600 * 24)
      hours = vessel["expires_in_seconds"] / 3600 % 24
      minutes = vessel["expires_in_seconds"] / 60 % 60
      vessel["expires_in"] = "%dd %dh %dm" % (days, hours, minutes)

  # return the used resources page constructed from a template
  return render_to_response('control/myvessels.html',
                            {'username' : user.username,
                             'num_vessels' : len(my_vessels),
                             'my_vessels' : my_vessels,
                             'sh_vessels' : shvessels,
                             'get_form' : get_form,
                             'action_summary' : action_summary,
                             'action_detail' : action_detail,
                             'my_donations' : len(my_donations),
                             'my_max_vessels' : my_max_vessels, 
                             'free_vessel_credits' : my_free_vessel_credits,
                             'total_vessel_credits' : my_total_vessel_credits,
                             'remove_summary' : remove_summary},
                        context_instance=RequestContext(request))
Exemple #3
0
def mygeni(request):
  try:
    user = _validate_and_get_geniuser(request)
  except LoggedInButFailedGetGeniUserError:
    return _show_failed_get_geniuser_page(request)
  
  total_vessel_credits = interface.get_total_vessel_credits(user)
  num_acquired_vessels = len(interface.get_acquired_vessels(user))
  avail_vessel_credits = interface.get_available_vessel_credits(user)
  
  if num_acquired_vessels > total_vessel_credits:
    percent_total_used = 100
    over_vessel_credits = num_acquired_vessels - total_vessel_credits
  else:
    percent_total_used = int((num_acquired_vessels * 1.0 / total_vessel_credits * 1.0) * 100.0)
    over_vessel_credits = 0
  
  # total_vessel_credits, percent_total_used, avail_vessel_credits
  return request_ro_response('control/mygeni.html',
                            {'username' : user.username,
                             'total_vessel_credits' : total_vessel_credits,
                             'used_vessel_credits' : num_acquired_vessels,
                             'percent_total_used' : percent_total_used,
                             'avail_vessel_credits' : avail_vessel_credits,
                             'over_vessel_credits' : over_vessel_credits},
                            context_instance=RequestContext(request))
Exemple #4
0
def mygeni(request):
  try:
    user = _validate_and_get_geniuser(request)
  except LoggedInButFailedGetGeniUserError:
    return _show_failed_get_geniuser_page(request)
  
  total_vessel_credits = interface.get_total_vessel_credits(user)
  num_acquired_vessels = len(interface.get_acquired_vessels(user))
  avail_vessel_credits = interface.get_available_vessel_credits(user)
  
  if num_acquired_vessels > total_vessel_credits:
    percent_total_used = 100
    over_vessel_credits = num_acquired_vessels - total_vessel_credits
  else:
    percent_total_used = int((num_acquired_vessels * 1.0 / total_vessel_credits * 1.0) * 100.0)
    over_vessel_credits = 0
  
  # total_vessel_credits, percent_total_used, avail_vessel_credits
  return request_ro_response('control/mygeni.html',
                            {'username' : user.username,
                             'total_vessel_credits' : total_vessel_credits,
                             'used_vessel_credits' : num_acquired_vessels,
                             'percent_total_used' : percent_total_used,
                             'avail_vessel_credits' : avail_vessel_credits,
                             'over_vessel_credits' : over_vessel_credits},
                            context_instance=RequestContext(request))
Exemple #5
0
def gen_get_form(geni_user, req_post=None):
    """
  <Purpose>
      Dynamically generates a GetVesselsForm that has the right
      number vessels (the allowed number of vessels a user may
      acquire). Possibly generate a GetVesselsForm from an HTTP POST
      request.

  <Arguments>
      geni_user:
          geni_user object
      req_post:
          An HTTP POST request (django) object from which a
          GetVesselsForm may be instantiated. If this argument is
          not supplied, a blank form will be created

  <Exceptions>
      None.

  <Side Effects>
      None.

  <Returns>
      A GetVesselsForm object that is instantiated with a req_post
      (if given).
  """

    # the total number of vessels a user may acquire
    avail_vessel_credits = interface.get_available_vessel_credits(geni_user)

    # Dynamic generation of the options for numbers the user can request based
    # on their number of available vessel credits.
    if avail_vessel_credits == 0:
        step = [0]
    elif avail_vessel_credits < 10:
        step = range(1, avail_vessel_credits + 1)
    elif avail_vessel_credits < 100:
        step = range(1, 10)
        step.extend(range(10, avail_vessel_credits + 1, 10))
    else:
        step = range(1, 10)
        step.extend(range(10, 101, 10))
        step.extend(range(200, avail_vessel_credits + 1, 100))

    if avail_vessel_credits not in step:
        step.append(avail_vessel_credits)

    # dynamically generate the get vessels form
    #get_vessel_choices = zip(range(1,max_num+1),range(1,max_num+1))
    get_vessel_choices = zip(step, step)

    # This is ugly (nested class definition, that is) and appears to have been
    # done as a way to avoid using a constructor but still make the value of
    # get_vessel_choices available to instances of this class.
    class GetVesselsForm(forms.Form):
        """
    <Purpose>
        Generates a form to acquire vessels by the user
    <Side Effects>
        None
    <Example Use>
        GetVesselsForm()
            to generate a blank form
        GetVesselsForm(post_request)
            to generate a form from an existing POST request
    """
        # maximum number of vessels a user is allowed to acquire
        #num = forms.ChoiceField(choices=get_vessel_choices, error_messages={'required' : 'Please enter the number of vessels to acquire'})
        num = forms.ChoiceField(choices=get_vessel_choices)

        # the various environment types the user may select from
        #env = forms.ChoiceField(choices=((1,'LAN'),(2,'WAN'),(3,'Random')), error_messages={'required' : 'Please enter the networking environment for vessels to acquire'})
        env = forms.ChoiceField(choices=(('wan', 'WAN'), ('lan', 'LAN'),
                                         ('nat', 'NAT'), ('rand', 'Random')))

        def clean_num(self):
            value = int(self.cleaned_data['num'])
            if value < 1:
                raise forms.ValidationError("Invalid vessel number selection.")
            return value

        def clean_env(self):
            value = str(self.cleaned_data['env'])
            if not (value == 'wan' or value == 'lan' or value == 'nat'
                    or value == 'rand'):
                raise forms.ValidationError("Invalid vessel type selection.")
            return value

        def get_errors_as_str(self):
            return str(self.errors)

    if req_post is None:
        return GetVesselsForm()
    return GetVesselsForm(req_post)
Exemple #6
0
def gen_get_form(geni_user, req_post=None):
  """
  <Purpose>
      Dynamically generates a GetVesselsForm that has the right
      number vessels (the allowed number of vessels a user may
      acquire). Possibly generate a GetVesselsForm from an HTTP POST
      request.

  <Arguments>
      geni_user:
          geni_user object
      req_post:
          An HTTP POST request (django) object from which a
          GetVesselsForm may be instantiated. If this argument is
          not supplied, a blank form will be created

  <Exceptions>
      None.

  <Side Effects>
      None.

  <Returns>
      A GetVesselsForm object that is instantiated with a req_post
      (if given).
  """
      
  # the total number of vessels a user may acquire
  avail_vessel_credits = interface.get_available_vessel_credits(geni_user)
  
  # Dynamic generation of the options for numbers the user can request based
  # on their number of available vessel credits.
  if avail_vessel_credits == 0:
    step = [0]
  elif avail_vessel_credits < 10:
    step = range(1, avail_vessel_credits+1)
  elif avail_vessel_credits < 100:
    step = range(1, 10)
    step.extend(range(10, avail_vessel_credits+1,10))
  else:
    step = range(1, 10)
    step.extend(range(10, 101,10))
    step.extend(range(200, avail_vessel_credits+1, 100))
    
  if avail_vessel_credits not in step:
    step.append(avail_vessel_credits)

  # dynamically generate the get vessels form
  #get_vessel_choices = zip(range(1,max_num+1),range(1,max_num+1))
  get_vessel_choices = zip(step, step)
  
  # This is ugly (nested class definition, that is) and appears to have been
  # done as a way to avoid using a constructor but still make the value of
  # get_vessel_choices available to instances of this class.
  class GetVesselsForm(forms.Form):
    """
    <Purpose>
        Generates a form to acquire vessels by the user
    <Side Effects>
        None
    <Example Use>
        GetVesselsForm()
            to generate a blank form
        GetVesselsForm(post_request)
            to generate a form from an existing POST request
    """
    # maximum number of vessels a user is allowed to acquire
    #num = forms.ChoiceField(choices=get_vessel_choices, error_messages={'required' : 'Please enter the number of vessels to acquire'})
    num = forms.ChoiceField(choices=get_vessel_choices)
    
    # the various environment types the user may select from
    #env = forms.ChoiceField(choices=((1,'LAN'),(2,'WAN'),(3,'Random')), error_messages={'required' : 'Please enter the networking environment for vessels to acquire'})
    env = forms.ChoiceField(choices=(('wan','WAN'),('lan','LAN'),('nat','NAT'),('rand','Random')))
    
    def clean_num(self):
      value = int(self.cleaned_data['num'])
      if value < 1:
        raise forms.ValidationError("Invalid vessel number selection.")
      return value
    
    def clean_env(self):
      value = str(self.cleaned_data['env'])
      if not (value == 'wan' or value == 'lan' or value == 'nat' or value == 'rand'):
        raise forms.ValidationError("Invalid vessel type selection.")
      return value
    
    def get_errors_as_str(self):
      return str(self.errors)
  
  if req_post is None:
      return GetVesselsForm()
  return GetVesselsForm(req_post)