def signup(request): context = u'signup' (valid, new_user, response) = prepare_status_for(request, context, forms.SignupForm) if valid: try: user = User.objects.get(username=new_user['username']) # If user already exists response['success'] = False response['errors'][context] = 'User already exists.' except User.DoesNotExist: # Creating a new user u = User() u.set_password(new_user['password']) u.username = new_user['username'] u.email = new_user['email'] u.first_name = new_user['first_name'] u.last_name = new_user['last_name'] try: u.save() response['success'] = True log.info("New user signup: %s" % (new_user['username'])); except: log.error("Error saving new user: %s" % (new_user['username'])); response['success'] = False response['errors'][context] = 'Error saving new user.' return http_wrap_json(response)
def add(user, url, title, timestamp=None, kind=None, description=None, content=None, tags=None, colour=None): d = Datasheet(user=user, url=url, title=unicode(title)) d.description = unicode(description) d.content = unicode(content) if timestamp: d.timestamp = timestamp if tags: d.tags = [unicode(tag) for tag in tags] if lookup_in_tuple(ADDITIONAL_TYPES, kind): d.kind = unicode(kind) if lookup_in_tuple(COLOUR_CODES, colour): d.colour = colour try: log.info('Saving highlight %s' % d) d.save() except: log.error('Failed to save highlight for user: %s' % user) # Updating the tag structures if tags: tc = TagContol(user) [tc.tagIncrement(tag) for tag in d.tags]
def delete(user, d_id): try: d = Datasheet.objects.get(user=user, id=d_id) # Updating tag structures tc = TagContol(user) # Decrement old tags (Tag Model) if d.tags: [tc.tagDecrement(tag) for tag in d.tags] d.delete() return True except: log.error('Failed to delete datasheet (%s) for user: %s' % (d_id, user)) return False
def modify(request): context=u'modify' (valid, user, response) = prepare_status_for(request, context, forms.ModifyUserForm) # To keep track of any changes changed = False if valid: u = auth.authenticate(username=user['username'], password=user['password']) if u is not None: # The following 'if' clauses prevent old data from being overwritten. if user['new_password']: changed = True u.set_password(user['new_password']) if user['new_username']: changed = True try: user = User.objects.get(username=user['new_username']) # If user already exists response['success'] = False response['errors'][context] = 'Username already exists.' return http_wrap_json(response) except User.DoesNotExist: u.username = user['new_username'] if user['new_email']: changed = True u.email = user['new_email'] if user['new_first_name']: changed = True u.first_name = user['new_first_name'] if user['new_last_name']: changed = True u.last_name = user['new_last_name'] try: log.info("Saving modified user (old username): %s" % user['username']) u.save() response[u'success'] = True except: log.error("Failed to save modified user (old username): %s" % user['username']) response['success'] = False response['errors'][context] = u'Failed to modify user details.' else: response['success'] = False response['errors'][context] = c.ERR_CREDNTIALS if not changed: response['success'] = False response['errors'][context] = c.ERR_NOTHING_CHANGED return http_wrap_json(response)
def delete_account(request): context=u'delete_account' (valid, user, response) = prepare_status_for(request, context, forms.DeleteAccountForm) if valid: u = auth.authenticate(username=user['username'], password=user['password']) if u is not None: try: log.info("Deleting user: %s" % user['username']) u.delete() response[u'success'] = True except: log.error("Failed to delete user: %s" % user['username']) response['success'] = False response['errors'][context] = c.ERR_CREDNTIALS else: response['success'] = False response['errors'][context] = c.ERR_CREDNTIALS return http_wrap_json(response)
def modify(user, d_id, url=None, title=None, kind=None, description=None, content=None, tags=None, colour=None): try: d = Datasheet.objects.get(id=d_id) except: return if url: d.url = unicode(url) if title: d.title = unicode(title) if lookup_in_tuple(ADDITIONAL_TYPES, kind): d.kind = unicode(kind) # The following can be empty d.description = unicode(description) d.content = unicode(content) # Updating tag structures tc = TagContol(user) # Decrement old tags (Tag Model) if d.tags: [tc.tagDecrement(tag) for tag in d.tags] # Updating to new tags (Webpage Model) if tags: d.tags = [unicode(tag) for tag in tags] else: d.tags = None # Increment new tags (Tag Model) if tags: [tc.tagIncrement(tag) for tag in tags] try: d.save() except: log.error('Failed to modify highlight for user: %s' % user)