def modify(request, page_number=0): context = 'modify' (valid, cleaned_data, response) = prepare_status_for(request, context, forms.EditForm) log.info("Modification request for: %s" % request.raw_post_data) if valid: log.info("Modification request received: %s" % cleaned_data) DatasheetControl.modify(user=request.user, d_id=cleaned_data['_id'], url=cleaned_data['url'], kind=cleaned_data['kind'], title=cleaned_data['title'], description=cleaned_data['description'], content=cleaned_data['content'], colour=cleaned_data['colour'], tags=cleaned_data['tags']) response['success'] = True response['roll'] = Datasheet.objects.get( id=cleaned_data['_id']).serialize_to_json() else: response['errors'][context] = "Failed to modify." response['success'] = False return http_wrap_json(response) return http_wrap_json(response)
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 json(request): log.info("Dashboard json requested") if request.is_ajax(): if request.method == 'POST': log.info('Posted Raw Data: "%s"' % request.raw_post_data) json_data = {"name": "mitthu"} # return HttpResponse(JSONEncoder().encode(json)) return HttpResponse(j.dumps(json_data), content_type="application/json")
def home(request): log.info("Dashboard home requested") return render_to_response( 'dashboard-index.html', { 'base_url': 'https://home.adityabasu.me/', 'portal_attr': 'tabindex="-1" target="_blank"' # For <a> tag }, RequestContext(request))
def username_available(request): context=u'username_available' (valid, user, response) = prepare_status_for(request, context, forms.UserAvailabilityForm) if valid: user = user[u'username'] response[u'for_username'] = user log.info(u"Checking user availability: %s" % user) try: user = User.objects.get(username=user) # If user already exists response[u'success'] = False response['errors'][context] = u'User already exists.' except User.DoesNotExist: response[u'success'] = True return http_wrap_json(response)
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 reset_password(request): context = u'reset_password' (valid, cleaned_data, response) = prepare_status_for(request, context, forms.ResetPasswordForm) if valid: try: user = User.objects.get(username=cleaned_data['username']) except User.DoesNotExist: response['success'] = False response['errors'][context] = 'No such user exists.' return http_wrap_json(response) if user.email != cleaned_data['email']: response['success'] = False response['errors'][context] = 'The entered email address does not match with our records.' return http_wrap_json(response) reset_password_for_user(user) log.info("Reset Password serviced for: %s" % (cleaned_data['username'])); return http_wrap_json(response)
def clean_tags(self): tags = self.cleaned_data['tags'] if not tags: return None tags_clean = tags.replace(', ', ',').replace(' ,', ',').replace(' , ', ',') if tags_clean[-1] == ',': tags_clean = tags_clean[:-1] tags_list = tags_clean.split(',') for tag in tags_list: log.info("Length of tag: %d" % tag.__len__()); if tag.__len__() > 30: raise forms.ValidationError("Tag length cannot be greater than 30 characters.") if not tag: tags_list.remove(tag) return tags_list
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 add(request, page_number=0): context = 'add' (valid, cleaned_data, response) = prepare_status_for(request, context, forms.AddForm) if valid: log.info("Data Received: %s" % cleaned_data) DatasheetControl.add(user=request.user, url=cleaned_data['url'], kind=cleaned_data['kind'], title=cleaned_data['title'], description=cleaned_data['description'], content=cleaned_data['content'], colour=cleaned_data['colour'], tags=cleaned_data['tags']) response['success'] = True else: response['errors'][context] = "Failed to insert into the database." response['success'] = False return http_wrap_json(response)
def get_highlights(request): log.info("Highlights requested") json_data = {"highlights": ['Maff', 'Uploads', 'Notes']} return HttpResponse(j.dumps(json_data), content_type="application/json")