def editEvent(request): ''' takes { id: string, name: string, start: string, end: string } returns { id: string, ... } ''' data = json.loads(request.POST['data']) event = get_object_or_404_json(models.Event, pk=ox.fromAZ(data['id'])) if event.editable(request.user): conflict = False conflict_names = [] names = [data.get('name', event.name)] + data.get( 'alternativeNames', []) for name in names: if models.Event.objects.filter( defined=True, name_find__icontains=u'|%s|' % name).exclude(id=event.id).count() != 0: conflict = True conflict_names.append(name) if not conflict: models.Event.objects.filter(defined=False, name__in=names).delete() for key in ('name', 'start', 'startTime', 'end', 'endTime', 'duration', 'durationTime', 'type', 'alternativeNames'): if key in data: value = data[key] if isinstance(value, basestring): value = ox.escape_html(value) if key == 'alternativeNames': value = tuple([ox.escape_html(v) for v in value]) setattr(event, key, value) if 'name' in data: event.set_name_sort(ox.escape_html(data['name'])) if 'nameSort' in data: event.set_name_sort(ox.escape_html(data['nameSort'])) event.save() if 'name' in data or 'alternativeNames' in data: event.update_matches() response = json_response(status=200, text='updated') response['data'] = event.json() else: response = json_response(status=409, text='Event name conflict') response['data']['names'] = conflict_names else: response = json_response(status=403, text='permission denied') return render_to_json_response(response)
def editUser(request): ''' takes { key: value } required key: id optional keys: username, email, level, notes returns { } ''' response = json_response() data = json.loads(request.POST['data']) user = get_object_or_404_json(User, pk=ox.fromAZ(data['id'])) profile = user.get_profile() if 'disabled' in data: user.is_active = not data['disabled'] if 'email' in data: if 'email' in data: data['email'] = ox.escape_html(data['email']) if User.objects.filter(email__iexact=data['email']).exclude( id=user.id).count() > 0: response = json_response(status=403, text='email already in use') return render_to_json_response(response) user.email = data['email'] if 'level' in data: profile.set_level(data['level']) if 'notes' in data: profile.notes = data['notes'] if 'newsletter' in data: profile.newsletter = data['newsletter'] if 'groups' in data: groups = data['groups'] if isinstance(groups, list): groups = filter(lambda g: g.strip(), groups) groups = [ox.escape_html(g) for g in groups] user.groups.exclude(name__in=groups).delete() current_groups = [g.name for g in user.groups.all()] for g in filter(lambda g: g not in current_groups, groups): group, created = Group.objects.get_or_create(name=g) user.groups.add(group) if 'username' in data: if User.objects.filter(username__iexact=data['username']).exclude( id=user.id).count() > 0: response = json_response(status=403, text='username already in use') return render_to_json_response(response) user.username = data['username'] user.save() profile.save() response['data'] = user.data.get().json() return render_to_json_response(response)
def editEvent(request): ''' takes { id: string, name: string, start: string, end: string } returns { id: string, ... } ''' data = json.loads(request.POST['data']) event = get_object_or_404_json(models.Event, pk=ox.fromAZ(data['id'])) if event.editable(request.user): conflict = False conflict_names = [] names = [data.get('name', event.name)] + data.get('alternativeNames', []) for name in names: if models.Event.objects.filter(defined=True, name_find__icontains=u'|%s|'%name).exclude(id=event.id).count() != 0: conflict = True conflict_names.append(name) if not conflict: models.Event.objects.filter(defined=False, name__in=names).delete() for key in ('name', 'start', 'startTime', 'end', 'endTime', 'duration', 'durationTime', 'type', 'alternativeNames'): if key in data: value = data[key] if isinstance(value, basestring): value = ox.escape_html(value) if key == 'alternativeNames': value = tuple([ox.escape_html(v) for v in value]) setattr(event, key, value) if 'name' in data: event.set_name_sort(ox.escape_html(data['name'])) if 'nameSort' in data: event.set_name_sort(ox.escape_html(data['nameSort'])) event.save() if 'name' in data or 'alternativeNames' in data: event.update_matches() response = json_response(status=200, text='updated') response['data'] = event.json() else: response = json_response(status=409, text='Event name conflict') response['data']['names'] = conflict_names else: response = json_response(status=403, text='permission denied') return render_to_json_response(response)
def editUser(request): ''' takes { key: value } required key: id optional keys: username, email, level, notes returns { } ''' response = json_response() data = json.loads(request.POST['data']) user = get_object_or_404_json(User, pk=ox.fromAZ(data['id'])) profile = user.get_profile() if 'disabled' in data: user.is_active = not data['disabled'] if 'email' in data: if 'email' in data: data['email'] = ox.escape_html(data['email']) if User.objects.filter(email__iexact=data['email']).exclude(id=user.id).count()>0: response = json_response(status=403, text='email already in use') return render_to_json_response(response) user.email = data['email'] if 'level' in data: profile.set_level(data['level']) if 'notes' in data: profile.notes = data['notes'] if 'newsletter' in data: profile.newsletter = data['newsletter'] if 'groups' in data: groups = data['groups'] if isinstance(groups, list): groups = filter(lambda g: g.strip(), groups) groups = [ox.escape_html(g) for g in groups] user.groups.exclude(name__in=groups).delete() current_groups = [g.name for g in user.groups.all()] for g in filter(lambda g: g not in current_groups, groups): group, created = Group.objects.get_or_create(name=g) user.groups.add(group) if 'username' in data: if User.objects.filter( username__iexact=data['username']).exclude(id=user.id).count()>0: response = json_response(status=403, text='username already in use') return render_to_json_response(response) user.username = data['username'] user.save() profile.save() response['data'] = user.data.get().json() return render_to_json_response(response)
def editPreferences(request): ''' takes { key: value } keys: email, password returns {} ''' data = json.loads(request.POST['data']) errors = {} change = False response = json_response() if 'email' in data: if User.objects.filter(email=data['email']).exclude( username=request.user.username).count() > 0: errors['email'] = 'Email address already in use' else: change = True request.user.email = ox.escape_html(data['email']) if 'newsletter' in data: profile = request.user.get_profile() profile.newsletter = data['newsletter'] profile.save() if 'password' in data: change = True request.user.set_password(data['password']) if change: request.user.save() if errors: response = json_response({'errors': errors}) return render_to_json_response(response)
def editPreferences(request): ''' takes { key: value } keys: email, password returns {} ''' data = json.loads(request.POST['data']) errors = {} change = False response = json_response() if 'email' in data: if User.objects.filter( email=data['email']).exclude(username=request.user.username).count()>0: errors['email'] = 'Email address already in use' else: change = True request.user.email = ox.escape_html(data['email']) if 'newsletter' in data: profile = request.user.get_profile() profile.newsletter = data['newsletter'] profile.save() if 'password' in data: change = True request.user.set_password(data['password']) if change: request.user.save() if errors: response = json_response({ 'errors': errors}) return render_to_json_response(response)
def cleanup_value(value, layer_type): #FIXME: what about other types? location etc if layer_type == 'text': value = ox.sanitize_html(value) else: value = ox.escape_html(value) return value
def addEvent(request): ''' takes { name: string, start: string, end: string } returns { id: string } ''' data = json.loads(request.POST['data']) existing_names = [] exists = False names = [data['name']] + data.get('alternativeNames', []) for name in names: name = ox.decode_html(name) if models.Event.objects.filter( defined=True, name_find__icontains=u'|%s|' % name).count() != 0: exists = True existing_names.append(name) if not exists: models.Event.objects.filter(defined=False, name__in=names).delete() data['name'] = ox.escape_html(data['name']) event = models.Event(name=data['name']) for key in ('start', 'startTime', 'end', 'endTime', 'duration', 'durationTime', 'type', 'alternativeNames'): if key in data and data[key]: value = data[key] if isinstance(value, basestring): value = ox.escape_html(value) if key == 'alternativeNames': value = tuple([ox.escape_html(v) for v in value]) setattr(event, key, value) if 'nameSort' in data: value = ox.escape_html(data['nameSort']) event.set_name_sort(value) event.matches = 0 event.save() event.update_matches() response = json_response(status=200, text='created') response['data'] = event.json() else: response = json_response(status=409, text='name exists') response['data']['names'] = existing_names return render_to_json_response(response)
def addEvent(request): ''' takes { name: string, start: string, end: string } returns { id: string } ''' data = json.loads(request.POST['data']) existing_names = [] exists = False names = [data['name']] + data.get('alternativeNames', []) for name in names: name = ox.decode_html(name) if models.Event.objects.filter(defined=True, name_find__icontains=u'|%s|'%name).count() != 0: exists = True existing_names.append(name) if not exists: models.Event.objects.filter(defined=False, name__in=names).delete() data['name'] = ox.escape_html(data['name']) event = models.Event(name=data['name']) for key in ('start', 'startTime', 'end', 'endTime', 'duration', 'durationTime', 'type', 'alternativeNames'): if key in data and data[key]: value = data[key] if isinstance(value, basestring): value = ox.escape_html(value) if key == 'alternativeNames': value = tuple([ox.escape_html(v) for v in value]) setattr(event, key, value) if 'nameSort' in data: value = ox.escape_html(data['nameSort']) event.set_name_sort(value) event.matches = 0 event.save() event.update_matches() response = json_response(status=200, text='created') response['data'] = event.json() else: response = json_response(status=409, text='name exists') response['data']['names'] = existing_names return render_to_json_response(response)
def signup(request): ''' takes { username: string, password: string, email: string } returns { errors: { username: '******', password: '******' } user: { ... } } ''' data = json.loads(request.POST['data']) if 'username' in data and 'password' in data: data['username'] = data['username'].strip() if 'email' in data: data['email'] = ox.escape_html(data['email']) if User.objects.filter(username__iexact=data['username']).count() > 0: response = json_response( {'errors': { 'username': '******' }}) elif User.objects.filter(email__iexact=data['email']).count() > 0: response = json_response( {'errors': { 'email': 'Email address already exists' }}) elif not data['password']: response = json_response( {'errors': { 'password': '******' }}) else: first_user = User.objects.count() == 0 user = User(username=data['username'], email=data['email']) user.set_password(data['password']) #make first user admin user.is_superuser = first_user user.is_staff = first_user user.save() #create default user lists: for l in settings.CONFIG['personalLists']: list = models.List(name=l['title'], user=user) for key in ('query', 'public', 'featured'): if key in l: setattr(list, key, l[key]) list.save() pos = models.Position(list=list, section='personal', user=user) qs = models.Position.objects.filter(user=user, section='personal') pos.position = (qs.aggregate(Max('position'))['position__max'] or 0) + 1 pos.save() if request.session.session_key: models.SessionData.objects.filter( session_key=request.session.session_key).update(user=user) ui = json.loads(request.session.get('ui', 'null')) user = authenticate(username=data['username'], password=data['password']) if ui: profile = user.get_profile() profile.ui = ui profile.save() login(request, user) user_json = models.init_user(user, request) response = json_response({'user': user_json}, text='account created') else: response = json_response(status=400, text='invalid data') return render_to_json_response(response)
def signup(request): ''' takes { username: string, password: string, email: string } returns { errors: { username: '******', password: '******' } user: { ... } } ''' data = json.loads(request.POST['data']) if 'username' in data and 'password' in data: data['username'] = data['username'].strip() if 'email' in data: data['email'] = ox.escape_html(data['email']) if User.objects.filter(username__iexact=data['username']).count() > 0: response = json_response({ 'errors': { 'username': '******' } }) elif User.objects.filter(email__iexact=data['email']).count() > 0: response = json_response({ 'errors': { 'email': 'Email address already exists' } }) elif not data['password']: response = json_response({ 'errors': { 'password': '******' } }) else: first_user = User.objects.count() == 0 user = User(username=data['username'], email=data['email']) user.set_password(data['password']) #make first user admin user.is_superuser = first_user user.is_staff = first_user user.save() #create default user lists: for l in settings.CONFIG['personalLists']: list = models.List(name=l['title'], user=user) for key in ('query', 'public', 'featured'): if key in l: setattr(list, key, l[key]) list.save() pos = models.Position(list=list, section='personal', user=user) qs = models.Position.objects.filter(user=user, section='personal') pos.position = (qs.aggregate(Max('position'))['position__max'] or 0) + 1 pos.save() if request.session.session_key: models.SessionData.objects.filter(session_key=request.session.session_key).update(user=user) ui = json.loads(request.session.get('ui', 'null')) user = authenticate(username=data['username'], password=data['password']) if ui: profile = user.get_profile() profile.ui = ui profile.save() login(request, user) user_json = models.init_user(user, request) response = json_response({ 'user': user_json }, text='account created') else: response = json_response(status=400, text='invalid data') return render_to_json_response(response)
def editPlace(request): ''' takes { id: string, name: string north: int } returns { names: [] } ''' data = json.loads(request.POST['data']) place = get_object_or_404_json(models.Place, pk=ox.fromAZ(data['id'])) names = data.get('name', []) if isinstance(names, basestring): names = [names] names = [ox.escape_html(n) for n in names] alternative_names = [ox.escape_html(n) for n in data.get('alternativeNames', [])] alternative_names = filter(lambda n: n.strip(), alternative_names) if place.editable(request.user): conflict = False conflict_names = [] conflict_geoname = '' if alternative_names: data['alternativeNames'] = alternative_names for name in names + alternative_names: name = ox.decode_html(name) if models.Place.objects.filter(defined=True, name_find__icontains=u'|%s|'%name).exclude(id=place.id).count() != 0: conflict = True conflict_names.append(name) ''' if 'geoname' in data: if models.Place.objects.filter(defined=True, geoname=data['geoname']).exclude(id=place.id).count() != 0: conflict = True conflict_geoname = data['geoname'] ''' if not conflict: models.Place.objects.filter(defined=False, name__in=names+alternative_names).delete() for key in data: if key != 'id': value = data[key] if isinstance(value, basestring): value = ox.escape_html(value) if isinstance(value, list): value = tuple(value) setattr(place, key, value) place.save() if 'name' in data or 'alternativeNames' in data: place.update_matches() response = json_response(place.json()) else: response = json_response(status=409, text='%s exists'%(conflict_names and 'Name' or 'Geoname')) response['data']['names'] = conflict_names if conflict_geoname: response['data']['geoname'] = conflict_geoname else: response = json_response(status=403, text='permission denied') return render_to_json_response(response)
def addPlace(request): ''' takes { name: "", alternativeNames: [], geoname: "", countryCode: '', south: float, west: float, north: float, east: float, lat: float, lng: float, area: float, type: "" } returns { id: string } ''' #FIXME: check permissions data = json.loads(request.POST['data']) exists = False existing_names = [] existing_geoname = '' name = data.pop('name') if name == '': _exists = True name = 'Untitled' n = 0 while _exists: _exists = models.Place.objects.filter(defined=True, name_find__icontains=u'|%s|'%name).count() > 0 if _exists: name = 'Untitled [%s]' %n n += 1 names = [name] + data.get('alternativeNames', []) data['alternativeNames'] = [ox.escape_html(n) for n in data.get('alternativeNames', [])] name = ox.escape_html(name) for n in names: n = ox.decode_html(name) if models.Place.objects.filter(defined=True, name_find__icontains=u'|%s|'%n).count() != 0: exists = True existing_names.append(n) ''' if 'geoname' in data: if models.Place.objects.filter(defined=True, geoname=data['geoname']).count() > 0: exists = True existing_geoname = data['geoname'] ''' if not exists: models.Place.objects.filter(defined=False, name__in=names).delete() place = models.Place() place.user = request.user place.name = name place.alternativeNames = tuple(data.pop('alternativeNames', [])) for key in data: value = data[key] if isinstance(value, list): value = tuple(value) setattr(place, key, value) place.matches = 0 place.save() place.update_matches() response = json_response(place.json()) else: response = json_response(status=409, text='%s exists'%(existing_names and 'Name' or 'Geoname')) response['data']['names'] = existing_names if existing_geoname: response['data']['geoname'] = existing_geoname return render_to_json_response(response)