def new_space(request): """ Display form for space **Context** space form **Template:** :template:`form_new_space.html` """ if request.method == 'POST': spaceUid = Space.objects.get(uid=request.POST.get('space')) space = Space(uid=uuid1(), name=request.POST.get('name'), description=request.POST.get('description'), type=request.POST.get('type'), status=1, parent=spaceUid) space.save() spaceUser = SpaceUser(uid=uuid1(), rights=owner_default_rights(), space=space, user=request.user) spaceUser.save() new_space_users_id = request.POST.get('new_space_users_id') if new_space_users_id: for user_id in new_space_users_id: spaceUser = SpaceUser(uid=uuid1(), rights=user_default_rights(), space=space, user=User.objects.get(id=user_id)) spaceUser.save() context = {'result': 'Success', 'space': str(space.uid)} else: context = None return HttpResponse(json.dumps(context), content_type="application/json")
def test_get_space_user_or_none(self): space = Space() space.save() user = User() user.save() self.assertEqual(get_space_user_or_none(user, space), None) space_user = SpaceUser(user=user, space=space) space_user.save() self.assertEqual(get_space_user_or_none(user, space), space_user)
def test_get_space_user(self): space = Space() space.save() user = User() user.save() with self.assertRaises(SpaceUser.DoesNotExist): get_space_user(user, space) space_user = SpaceUser(user=user, space=space) space_user.save() self.assertEqual(get_space_user_or_none(user, space), space_user)
def test_user_see_child_02(self): user = User() user.save() parent = Space() parent.save() child = Space(type=1) child.parent = parent child.save() space_user = SpaceUser() space_user.user = user space_user.space = parent space_user.save() self.assertTrue(user_see_child(user, space_user, child))
def register(request): """ Display form for registration **Context** registration form **Template:** :template:`register.html` """ if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): try: salt = hashlib.sha1(str(random()).encode("utf-8")).hexdigest()[:5] activation_key = hashlib.sha1((salt + form.cleaned_data["email"]).encode("utf-8")).hexdigest() key_expires = datetime.datetime.now() + datetime.timedelta(2) email_subject = 'Embigo - Potwierdzenie Rejestracji ' email_body = "Witaj %s, dziękujemy za rejestrację w Embigo. By zakończyć proces rejestracji musisz, w przeciągu" \ " 48 godzin kliknąć w poniższy link:\nhttp://%s/confirm/%s" % ( form.cleaned_data["username"], request.META["HTTP_HOST"], activation_key) send_mail(email_subject, email_body, '*****@*****.**', [form.cleaned_data["email"]], fail_silently=False) new_user = form.save() new_user.is_active = False new_user.save() new_space_user = SpaceUser(uid=uuid1(), rights=embigo_default_rights(), space=embigo_main_space(), user=new_user) new_space_user.save() colors = ["#FEE6AA","#FDD777","#FCC845","#FBBA13","#FEAAAA","#FD7777","#FC4545","#FB1313","#EBAAFE","#E077FD","#D545FC","#C913FB","#B9AAFE","#9077FD","#6745FC","#3E13FB","#AAE0FE","#77CEFD","#45BCFC","#13AAFB","#AAFECA","#77FDAB","#45FC8B","#13FB6C"] color = random.sample(colors, 1)[0]; embigo_user = EmbigoUser(user=new_user, activation_key=activation_key, key_expires=key_expires, hash_type='ACTIVATE_HASH') embigo_user.save() return HttpResponseRedirect(request.GET.get("next", "/"), RequestContext(request)) except SMTPRecipientsRefused: form.add_error("email","Błąd podczas wysyłania maila. Upewnij się czy wprowadzony adres e-mail jest poprawny.") except Exception as e: new_user.delete() raise e else: form = RegistrationForm() context = {'form': form} return render(request, 'register.html', context)
def add_collaborators(request): """ Display form for adding collaborators **Context** add collaborators form **Template:** :template:`form_collaborators.html` """ if request.method == 'POST': space = Space.objects.get(uid=request.POST.get('space')) for user_id in request.POST.getlist('new_collaborators_id[]'): spaceUser = SpaceUser(uid=uuid1(), rights=user_default_rights(), space=space, user=User.objects.get(id=user_id)) spaceUser.save() context = {'result': 'Success'} else: context = None return HttpResponse(json.dumps(context), content_type="application/json")
def index(request): """ homepage redirect start space """ try: emgibo_space = Space.objects.get(uid='00000000-0000-0000-0000-000000000000') except Space.DoesNotExist: create_embigo_space() emgibo_space = Space.objects.get(uid='00000000-0000-0000-0000-000000000000') try: get_space_user(request.user, emgibo_space) except SpaceUser.DoesNotExist: space_user = SpaceUser( uid=uuid1(), rights=embigo_default_rights(), space=emgibo_space, user=request.user, ) space_user.save() if request.get_full_path() == '/00000000-0000-0000-0000-000000000000/': return HttpResponseRedirect("/") else: return space(request)
def space(request, space_id='00000000-0000-0000-0000-000000000000'): """ Display a space **Context** 'space': this space 'own_spaces': list of login user's spaces 'other_spaces': list of login user's spaces witch user can see 'collaborators': list of space's users 'messages': list of space's messages 'can_add_message': rights flag 'can_create_space': rights flag 'can_edit_space': rights flag 'can_archive_space': rights flag 'can_delete_space': rights flag 'can_add_user:'******'can_edit_user_rights': rights flag **Template:** :template:`space.html` """ user = request.user space = get_object_or_404(Space, pk=space_id) if user_see_space(user, space): try: space_user = get_space_user(user, space) except SpaceUser.DoesNotExist: if space.is_embigo_space(): rights = embigo_default_rights() else: rights = user_default_rights_of_public_space() space_user = SpaceUser(uid=uuid1(), rights=rights, space=space, user=user) space_user.save() children_list = [c for c in space.children.all() if c.is_active()] user_spaces = {} for c in children_list: if user_see_child(user, space_user, c): c_user = get_space_user_or_none(user, c) user_spaces[c] = [gc for gc in c.children.all() if gc.is_active() and user_see_child(user, c_user, gc)] parent_collaborators = [] if space.parent: try: parent_user = SpaceUser.objects.get(space=space.parent, user=user) except SpaceUser.DoesNotExist: parent_user = None if parent_user and parent_user.can(SEE_USERS): parent_collaborators = SpaceUser.objects.filter(space=space.parent) parent_collaborators = [pc for pc in parent_collaborators if not user_is_space_user(pc.user, space) and space_user.can(SEE_USERS)] can_add_message = space_user.can(ADD_MESSAGE) can_create_space = space_user.can(CREATE_SPACE) can_edit_space = space_user.can(EDIT_SPACE) can_archive_space = space_user.can(ARCHIVE_SPACE) can_delete_space = space_user.can(DELETE_SPACE) can_add_user = space_user.can(ADD_USER) can_edit_user_rights = space_user.can(EDIT_RIGHTS) can_see_users = space_user.can(SEE_USERS) user_key = encrypt(SECRET_KEY_WEBSOCKET, request.session.session_key) websocket_ip = urlparse("http://" + request.META["HTTP_HOST"]).hostname websocket_server_address = 'ws://' + websocket_ip + ':' + str(WEBSOCKET_PORT) + '/'; context = { 'space': space, 'user_spaces': user_spaces, 'parent_collaborators': parent_collaborators, 'can_add_message': can_add_message, 'can_create_space': can_create_space, 'can_edit_space': can_edit_space, 'can_archive_space': can_archive_space, 'can_delete_space': can_delete_space, 'can_add_user': can_add_user, 'can_edit_user_rights': can_edit_user_rights, 'can_see_users': can_see_users, 'user_key': user_key, 'websocket_server_address': websocket_server_address } return render(request, 'space.html', context) else: return HttpResponseRedirect("/")