예제 #1
0
파일: views.py 프로젝트: aaltsmienk/amcat
def login(request):
    """
    Try to log a user in. If method is GET, show empty login-form
    """
    if not request.user.is_authenticated() and request.user.is_active:
        # User already logged in
        return _redirect_login(request)

    system = AmCAT.get_instance()
    announcements = [system.server_warning, system.global_announcement]

    announcement = "<hr/>".join(a for a in announcements if a is not None)

    if request.method == "POST":
        username = request.POST.get("username")
        passwd = request.POST.get("password")

        user = authenticate(username=username, password=passwd)

        if user is None or not user.is_active:
            # Credentials wrong or account disabled
            return _login(request, True, username, announcement)

        # Credentials OK, log user in
        auth_login(request, user)
        signals.user_logged_in.send(sender=user.__class__, request=request, user=user)
        log.info(json.dumps({"action": "login", "user": username}))
        return _redirect_login(request)

    # GET request, send empty form
    return _login(request, False, None, announcement)
예제 #2
0
def login(request):
    """
    Try to log a user in. If method is GET, show empty login-form
    """
    if not request.user.is_authenticated() and request.user.is_active:
        # User already logged in
        return _redirect_login(request)

    system = AmCAT.get_instance()
    announcements = [system.server_warning, system.global_announcement]
    
    announcement = "<hr/>".join(a for a in announcements if a is not None)

    if request.method == "POST":
        username = request.POST.get("username")
        passwd = request.POST.get("password")

        user = authenticate(username=username, password=passwd)

        if user is None or not user.is_active:
            # Credentials wrong or account disabled
            return _login(request, True, username, announcement)

        # Credentials OK, log user in
        auth_login(request, user)
        signals.user_logged_in.send(sender=user.__class__, request=request, user=user)
        return _redirect_login(request)

    # GET request, send empty form
    return _login(request, False, None, announcement)
예제 #3
0
def extra(request):
    announcement = get_announcement(request)
    warning = AmCAT.get_instance().server_warning
    notice = session_pop(request.session, "notice")
    if request.user.is_anonymous():
        theme = 'amcat'
    else:
        theme = getattr(request.user.get_profile(), 'theme', 'amcat').lower().replace(" ", "_")
    return locals()
예제 #4
0
파일: test_amcat.py 프로젝트: BBie/amcat
    def test_get_instance(self):
        a = AmCAT.get_instance()
        self.assertEqual(type(a), AmCAT)

        os.environ['AMCAT_SERVER_STATUS'] = ""
        self.assertEqual(a.server_warning,
                         WARNING_NOT_PRODUCTION.format(server="not the production server"))

        os.environ['AMCAT_SERVER_STATUS'] = "production"
        self.assertEqual(a.server_warning, None)
예제 #5
0
def upgrade_database():
    # create uuid extension if needed
    if connections.databases['default']["ENGINE"] == 'django.db.backends.postgresql_psycopg2':
        execute_sql_file("uuid_extension.sql")

    version = AmCAT.get_instance().db_version
    if version == CURRENT_DB_VERSION:
        return  # early exit to avoid spurious log message

    while version < CURRENT_DB_VERSION:
        log.info("Upgrading from version {version}".format(**locals()))
        _upgrade_from(version)

        new_version = AmCAT.get_instance().db_version
        if version >= new_version:
            raise Exception("No version progress after calling upgrade ({version}>={new_version})".format(**locals()))
        version = new_version

    log.info("Succesfully upgraded database to version {version}".format(**locals()))
def extra(request):
    try:
        announcement = AmCAT.get_instance().global_announcement
    except:
        log.exception("Cannot get announcement")
        return dict(request=request)
    
    last_announcement = request.session.get(ANNOUNCE_KEY)
    count = int(request.session.get(COUNT_KEY, 0)) + 1

    if last_announcement == announcement and count >= DISPLAY_COUNT:
        announcement = None
    elif last_announcement != announcement:
        request.session["last_announcement"] = announcement
        count = 0

    if count < DISPLAY_COUNT:
        request.session[COUNT_KEY] = count

    return dict(request=request, warning=AmCAT.get_instance().server_warning, announcement=announcement)
예제 #7
0
    def test_get_instance(self):
        a = AmCAT.get_instance()
        self.assertEqual(type(a), AmCAT)

        os.environ['AMCAT_SERVER_STATUS'] = ""
        self.assertEqual(
            a.server_warning,
            WARNING_NOT_PRODUCTION.format(server="not the production server"))

        os.environ['AMCAT_SERVER_STATUS'] = "production"
        self.assertEqual(a.server_warning, None)
예제 #8
0
def extra(request):
    try:
        announcement = AmCAT.get_instance().global_announcement
    except:
        log.exception("Cannot get announcement")
        return dict(request=request)

    last_announcement = request.session.get(ANNOUNCE_KEY)
    count = int(request.session.get(COUNT_KEY, 0)) + 1

    if last_announcement == announcement and count >= DISPLAY_COUNT:
        announcement = None
    elif last_announcement != announcement:
        request.session["last_announcement"] = announcement
        count = 0

    if count < DISPLAY_COUNT:
        request.session[COUNT_KEY] = count

    return dict(request=request,
                warning=AmCAT.get_instance().server_warning,
                announcement=announcement)
예제 #9
0
def extra(request):
    announcement = get_announcement(request)
    warning = AmCAT.get_instance().server_warning
    notice = session_pop(request.session, "notice")

    # Set theme
    if hasattr(request, 'user'):
        if request.user.is_anonymous():
            theme = 'amcat'
        else:
            theme = getattr(request.user.userprofile, 'theme', 'amcat').lower().replace(" ", "_")

    # Set cache token to be used by requirejs
    cache_bust_token = settings.CACHE_BUST_TOKEN

    return locals()
예제 #10
0
파일: context.py 프로젝트: aemal/amcat
def get_announcement(request):
    """
    A announcement can be placed in the amcat_system table, which will be displayed
    to each user DISPLAY_COUNT times.
    """
    announcement = AmCAT.get_instance().global_announcement
    last_announcement = request.session.get(ANNOUNCE_KEY)
    count = int(request.session.get(COUNT_KEY, 0)) + 1

    if last_announcement == announcement and count >= DISPLAY_COUNT:
        announcement = None
    elif last_announcement != announcement:
        request.session["last_announcement"] = announcement
        count = 0

    if count < DISPLAY_COUNT:
        request.session[COUNT_KEY] = count

    return announcement
예제 #11
0
def get_announcement(request):
    """
    A announcement can be placed in the amcat_system table, which will be displayed
    to each user DISPLAY_COUNT times.
    """
    announcement = AmCAT.get_instance().global_announcement
    last_announcement = request.session.get(ANNOUNCE_KEY)
    count = int(request.session.get(COUNT_KEY, 0)) + 1

    if last_announcement == announcement and count >= DISPLAY_COUNT:
        announcement = None
    elif last_announcement != announcement:
        request.session["last_announcement"] = announcement
        count = 0

    if count < DISPLAY_COUNT:
        request.session[COUNT_KEY] = count

    return announcement