def duplicate_project(request): if request.user is None or request.user.is_anonymous(): return HttpResponseText('error') proj_id_to_copy = json.loads(request.POST.get('project_id', None)) worked = _unchecked_duplicate_project(request, proj_id_to_copy) if worked: return HttpResponseText('ok') else: return HttpResponseText('too_many')
def delete_set_post(request): set_id = safe_int(request.POST.get('setId', None)) if not set_id: return HttpResponseText('not ok') worked = _delete_set(set_id) if not worked: return HttpResponseText('not ok') return HttpResponseText('ok')
def delete_item_post(request): item_id = safe_int(request.POST.get('itemId', None)) if not item_id: return HttpResponseText('not ok') worked = _delete_item(item_id) if not worked: return HttpResponseText('not ok') return HttpResponseText('ok')
def change_password(request): username = request.POST.get('username', None) password = request.POST.get('password', None) new_password = request.POST.get('newPassword', None) user = authenticate(username=username, password=password) if user is not None: user.set_password(new_password) user.save() return HttpResponseText('ok') else: return HttpResponseText('invalid')
def local_login(request): username = request.POST.get('username', None) password = request.POST.get('password', None) user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) return HttpResponseText('ok') else: return HttpResponseText('confirm-email') else: return HttpResponseText('invalid')
def update_item_post(request): item = json.loads(request.POST.get('item', None)) if item == None: return HttpResponseText('not ok') item_id = safe_int(item['id']) if item_id: new_item, new_weapon = _convert_json_item_to_item(item) old_item = get_structure().get_item_by_id(item_id) update_item(old_item, new_item, new_weapon, True) return HttpResponseText('ok') else: _insert_item(item) return HttpResponseText('ok')
def update_set_post(request): s = json.loads(request.POST.get('set', None)) if s == None: return HttpResponseText('not ok') set_id = safe_int(s['id']) if set_id: new_set = _convert_json_set_to_set(s) worked = update_set(set_id, new_set) if worked: return HttpResponseText('ok') else: return HttpResponseText('not ok') else: _insert_set(s) return HttpResponseText('ok')
def hide_sharing_link(request, char_id): char = get_char_or_raise(request, char_id) char.link_shared = False char.save() return HttpResponseText('hid')
def get_sharing_link(request, char_id): char = get_char_or_raise(request, char_id) char.link_shared = True char.save() return HttpResponseText(generate_link(char))
def delete_projects(request): projects_json = request.POST.get('projects', None) projects = json.loads(projects_json) for proj_id in projects: char = get_char_or_raise(request, proj_id) char.deleted = True char.save() if 'char_id' in request.session: del request.session['char_id'] return HttpResponseText('ok')
def confirm_email(request, username, confirmation_token): if confirmation_token != _generate_token_for_user(username): return HttpResponseText('invalid token') users = User.objects.filter(username=username) if not users or len(users) != 1: return HttpResponseText('invalid token') user = users[0] if user.is_active: return HttpResponseRedirect( reverse('chardata.login_view.email_confirmed_page', args=(username, 'yes'))) user.is_active = True user.save() return HttpResponseRedirect( reverse('chardata.login_view.email_confirmed_page', args=(username, 'no')))
def remove_item(request, char_id): char = get_char_or_raise(request, char_id) slot = request.POST.get('slot', None) assert slot in SLOTS result = get_solution(char) result.switch_item(None, slot) set_solution(char, result) remove_cache_for_char(char_id) return HttpResponseText('ok')
def save_project_to_user(request): if 'char_id' in request.session and not request.user.is_anonymous(): char = get_object_or_404(Char, pk=request.session['char_id']) if request.user is not None and not request.user.is_anonymous(): chars = Char.objects.filter(owner=request.user) chars = chars.exclude(deleted=True) if len( chars ) < MAXIMUM_NUMBER_OF_PROJECTS or request.user.email in TESTER_USERS: char.owner = request.user char.save() del request.session['char_id'] return HttpResponseText('ok')
def switch_item(request, char_id): char = get_char_or_raise(request, char_id) item_name = request.POST.get('itemName', None) slot = request.POST.get('slot', None) assert slot in SLOTS structure = get_structure() result = get_solution(char) result.switch_item(structure.get_item_by_id(int(item_name)), slot) set_solution(char, result) remove_cache_for_char(char_id) return HttpResponseText('ok')
def choose_compare_sets_post(request): links_json = request.POST.get('links', None) links = json.loads(links_json) links_digested = [_process_link(l) for l in links] if len(links_digested) <= 1: return _get_text_error_response( _('Paste links of at least 2 projects to compare')) # Validation char_ids = [] for i, mystery_char_id in enumerate(links_digested): if mystery_char_id.isdigit(): char_id = int(mystery_char_id) char = get_or_none(Char, pk=char_id) if not char: return _get_text_error_response( _('%s does not refer to a valid project') % links[i]) if not char_belongs_to_user(request, char): return _get_text_error_response( _('%s refers to someone else\'s project') % links[i]) char_ids.append(mystery_char_id) else: try: char_id = decode_char_id(mystery_char_id) except: char_id = None if char_id is None: return _get_text_error_response( _('%s is not a valid share link') % links[i]) char = get_or_none(Char, pk=char_id) if not char.link_shared: return _get_text_error_response( _('%s is not shared') % links[i]) char_ids.append('s' + mystery_char_id) compare_path = '/'.join(char_ids) return HttpResponseText( reverse('chardata.compare_sets_view.compare_sets', args=(compare_path, )))
def get_sharing_link(request, sets_params): char_strs = _process_parameters(sets_params) char_ids = [] for char_str in char_strs: char_id, was_encoded = get_char_id_possibly_encoded(char_str) char = get_object_or_404(Char, pk=char_id) if char_belongs_to_user(request, char): # Share it, if still not shared. if not char.link_shared: char.link_shared = True char.save() else: # Verify it had a signature and was shared. if not was_encoded: raise PermissionDenied if not char.link_shared: return _get_text_error_response( _('Project %s is not shared.') % char_str) char_ids.append(char_id) return HttpResponseText(_generate_share_compare_link(char_ids))
def set_item_forbidden(request, char_id): char = get_char_or_raise(request, char_id) slot = request.POST.get('slot', None) item_name = request.POST.get('equip', None) forbidden = request.POST.get('forbidden', None) structure = get_structure() item = structure.get_item_by_name(item_name) if item is None: or_item = structure.get_or_item_by_name(item_name) item_id = or_item[0].id else: item_id = structure.get_item_by_name(item_name).id if forbidden == 'true': set_excluded(char, item_id, True) elif forbidden == 'false': set_excluded(char, item_id, False) return HttpResponseText('char_id %s, slot %s, equip %s, forbidden %s' % (char_id, slot, item_name, str(forbidden)))
def set_item_locked(request, char_id): char = get_char_or_raise(request, char_id) slot = request.POST.get('slot', None) item_name = request.POST.get('equip', None) locked = request.POST.get('locked', None) assert slot in SLOTS structure = get_structure() item = structure.get_item_by_name(item_name) if item is None: or_item = structure.get_or_item_by_name(item_name) item_id = or_item[0].id else: item_id = structure.get_item_by_name(item_name).id if locked == 'true': set_item_included(char, item_id, slot, True) elif locked == 'false': set_item_included(char, item_id, slot, False) return HttpResponseText('char_id %s, slot %s, equip %s, locked %s' % (char_id, slot, item_name, str(locked)))
def _get_text_error_response(cause): return HttpResponseText('Error: %s' % cause)
def check_if_taken(request): username = request.POST.get('username', None) users = User.objects.filter(username=username) if users: return HttpResponseText('username-error') return HttpResponseText('ok')