Skip to content

Instantly share code, notes, and snippets.

@rnagle
Last active November 10, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnagle/1eb6e704d6903983e902 to your computer and use it in GitHub Desktop.
Save rnagle/1eb6e704d6903983e902 to your computer and use it in GitHub Desktop.
Import Newslynx users from CSV
import csv
import re
from unicodedata import normalize
from newslynx.client import API
_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
def main():
api = API()
with open('newslynx_users.csv') as csvfile:
"""
The CSV should have at least the following column headers:
Organization,Contact name,Email
"""
users = [row for row in csv.DictReader(csvfile)]
orgs = set([row.get('Organization') for row in users])
for org in orgs:
org_data = {
'slug': slugify(unicode(org)),
'name': org,
'timezone': 'UTC'
}
new_org = api.orgs.create(**org_data)
org_users = filter(lambda x: x['Organization'] == org, users)
for org_user in org_users:
api.orgs.create_user(
new_org.get('id'),
name=org_user.get('Contact name'),
email=org_user.get('Email'),
password=slugify(unicode(org_user.get('Contact name') + '1234')),
admin=False
)
def slugify(text, delim=u'-'):
"""Generates an slightly worse ASCII-only slug."""
result = []
for word in _punct_re.split(text.lower()):
word = normalize('NFKD', word).encode('ascii', 'ignore')
if word:
result.append(word)
return unicode(delim.join(result))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment