import ckan.logic as logic
import ckan.plugins.toolkit as toolkit


def event_create(context, data_dict):
    """ Creates a 'event' type group with a custom unique identifier for the
    event """

    if data_dict.get('name'):
        name = data_dict.get('name')
    else:
        # Generate a new operation ID
        existing_events = toolkit.get_action('group_list')(
                context,
                {'type': 'event', 'sort': 'name desc'})

        if len(existing_events) == 0:
            event_code = 1
        else:
            event_code = int(existing_events[0]) + 1

        name = str(event_code).zfill(5)

    data_dict.update({
        'name': name,
        'type':'event'
        })

    try:
        foo = toolkit.get_action('group_create')(
            context,
            data_dict=data_dict)
    except (logic.NotFound) as e:
        raise toolkit.ValidationError("foo %s" % e)

    return foo

import ckan.logic as logic
import ckan.plugins.toolkit as toolkit


def event_create(context, data_dict):
    """ Creates a 'event' type group with a custom unique identifier for the
    event """

    if data_dict.get('name'):
        name = data_dict.get('name')
    else:
        # Generate a new operation ID
        existing_events = toolkit.get_action('group_list')(
                context,
                {'type': 'event', 'sort': 'name desc'})


        event_code = 1  #default value, if there are no existing numericly named events

        for event in existing_events:
            if event.isdigit():
                event_code = int(event) + 1
                break

        name = str(event_code).zfill(5)

    data_dict.update({
        'name': name,
        'type':'event'
        })

    try:
        foo = toolkit.get_action('group_create')(
            context,
            data_dict=data_dict)
    except (logic.NotFound) as e:
        raise toolkit.ValidationError("foo %s" % e)

    return foo

Make auto-incrementing event names work with a mixture of numeric and non-numeric event names
Make auto-incrementing event names work with a mixture of numeric and non-numeric event names

  import ckan.logic as logic
  import ckan.plugins.toolkit as toolkit
  
  
  def event_create(context, data_dict):
      """ Creates a 'event' type group with a custom unique identifier for the
      event """
  
      if data_dict.get('name'):
          name = data_dict.get('name')
      else:
          # Generate a new operation ID
          existing_events = toolkit.get_action('group_list')(
                  context,
                  {'type': 'event', 'sort': 'name desc'})
  
-         if len(existing_events) == 0:
-             event_code = 1
-         else:
+ 
+         event_code = 1  #default value, if there are no existing numericly named events
+ 
+         for event in existing_events:
+             if event.isdigit():
-             event_code = int(existing_events[0]) + 1
+                 event_code = int(event) + 1
+                 break
  
          name = str(event_code).zfill(5)
  
      data_dict.update({
          'name': name,
          'type':'event'
          })
  
      try:
          foo = toolkit.get_action('group_create')(
              context,
              data_dict=data_dict)
      except (logic.NotFound) as e:
          raise toolkit.ValidationError("foo %s" % e)
  
      return foo
  
Make auto-incrementing event names work with a mixture of numeric and non-numeric event names
## Code Before:
import ckan.logic as logic
import ckan.plugins.toolkit as toolkit


def event_create(context, data_dict):
    """ Creates a 'event' type group with a custom unique identifier for the
    event """

    if data_dict.get('name'):
        name = data_dict.get('name')
    else:
        # Generate a new operation ID
        existing_events = toolkit.get_action('group_list')(
                context,
                {'type': 'event', 'sort': 'name desc'})

        if len(existing_events) == 0:
            event_code = 1
        else:
            event_code = int(existing_events[0]) + 1

        name = str(event_code).zfill(5)

    data_dict.update({
        'name': name,
        'type':'event'
        })

    try:
        foo = toolkit.get_action('group_create')(
            context,
            data_dict=data_dict)
    except (logic.NotFound) as e:
        raise toolkit.ValidationError("foo %s" % e)

    return foo

## Instruction:
Make auto-incrementing event names work with a mixture of numeric and non-numeric event names
## Code After:
import ckan.logic as logic
import ckan.plugins.toolkit as toolkit


def event_create(context, data_dict):
    """ Creates a 'event' type group with a custom unique identifier for the
    event """

    if data_dict.get('name'):
        name = data_dict.get('name')
    else:
        # Generate a new operation ID
        existing_events = toolkit.get_action('group_list')(
                context,
                {'type': 'event', 'sort': 'name desc'})


        event_code = 1  #default value, if there are no existing numericly named events

        for event in existing_events:
            if event.isdigit():
                event_code = int(event) + 1
                break

        name = str(event_code).zfill(5)

    data_dict.update({
        'name': name,
        'type':'event'
        })

    try:
        foo = toolkit.get_action('group_create')(
            context,
            data_dict=data_dict)
    except (logic.NotFound) as e:
        raise toolkit.ValidationError("foo %s" % e)

    return foo
