Esempio n. 1
0
def spam_dashboard(request):

    from django.contrib.auth.models import User

    if request.method == 'POST':

        spam_sheet_ids = list(map(int, request.POST.getlist("spam_sheets[]", [])))
        reviewed_sheet_ids = list(map(int, request.POST.getlist("reviewed_sheets[]", [])))

        db.sheets.update_many({"id": {"$in": reviewed_sheet_ids}}, {"$set": {"reviewed": True}})

        spammers = db.sheets.find({"id": {"$in": spam_sheet_ids}}, {"owner": 1}).distinct("owner")

        for spammer in spammers:
            try:
                spammer_account = User.objects.get(id=spammer)
                spammer_account.is_active = False
                spammer_account.save()
            except:
                continue

        db.sheets.delete_many({"id": {"$in": spam_sheet_ids}})

        return render_template(request, 'spam_dashboard.html', None, {
            "deleted_sheets": len(spam_sheet_ids),
            "sheet_ids": spam_sheet_ids,
            "reviewed_sheets": len(reviewed_sheet_ids),
            "spammers_deactivated": len(spammers)
        })

    else:
        date = request.GET.get("date", None)

        if date:
            date = datetime.strptime(date, '%Y-%m-%d')

        else:
            date = request.GET.get("date", datetime.now() - timedelta(days=30))

        earliest_new_user_id = User.objects.filter(date_joined__gte=date)[0].id

        regex = r'.*(?!href=[\'"](\/|http(s)?:\/\/(www\.)?sefaria).+[\'"])(href).*'
        sheets = db.sheets.find({"sources.ref": {"$exists": False}, "dateCreated": {"$gt": date.strftime("%Y-%m-%dT%H:%M:%S.%f")}, "owner": {"$gt": earliest_new_user_id}, "includedRefs": {"$size": 0}, "reviewed": {"$ne": True}, "$or": [{"sources.outsideText": {"$regex": regex}}, {"sources.comment": {"$regex": regex}}, {"sources.outsideBiText.en": {"$regex": regex}}, {"sources.outsideBiText.he": {"$regex": regex}}]})

        sheets_list = []

        for sheet in sheets:
            sheets_list.append({"id": sheet["id"], "title": strip_tags(sheet["title"]), "owner": user_link(sheet["owner"])})

        return render_template(request, 'spam_dashboard.html', None, {
            "title": "Potential Spam Sheets since %s" % date.strftime("%Y-%m-%d"),
            "sheets": sheets_list,
        })
Esempio n. 2
0
def register(request):
    if request.user.is_authenticated:
        return redirect("login")

    next = request.GET.get('next', '')

    if request.method == 'POST':
        errors, _, form = process_register_form(request)
        if len(errors) == 0:
            if "noredirect" in request.POST:
                return HttpResponse("ok")
            elif "new?assignment=" in request.POST.get("next",""):
                next = request.POST.get("next", "")
                return HttpResponseRedirect(next)
            else:
                next = request.POST.get("next", "/")
                if "?" in next:
                    next += "&welcome=to-sefaria"
                else:
                    next += "?welcome=to-sefaria"
                return HttpResponseRedirect(next)
    else:
        if request.GET.get('educator', ''):
            form = SefariaNewUserForm(initial={'subscribe_educator': True})
        else:
            form = SefariaNewUserForm()

    return render_template(request, "registration/register.html", None, {'form': form, 'next': next})
Esempio n. 3
0
def sheet_spam_dashboard(request):

    from django.contrib.auth.models import User

    if request.method == 'POST':
        return jsonResponse({"error": "Unsupported Method: {}".format(request.method)})

    else:
        date = request.GET.get("date", None)

        if date:
            date = datetime.strptime(date, '%Y-%m-%d')

        else:
            date = request.GET.get("date", datetime.now() - timedelta(days=30))

        earliest_new_user_id = User.objects.filter(date_joined__gte=date).order_by('date_joined')[0].id

        regex = r'.*(?!href=[\'"](\/|http(s)?:\/\/(www\.)?sefaria).+[\'"])(href).*'
        sheets = db.sheets.find({"sources.ref": {"$exists": False}, "dateCreated": {"$gt": date.strftime("%Y-%m-%dT%H:%M:%S.%f")}, "owner": {"$gt": earliest_new_user_id}, "includedRefs": {"$size": 0}, "reviewed": {"$ne": True}, "$or": [{"sources.outsideText": {"$regex": regex}}, {"sources.comment": {"$regex": regex}}, {"sources.outsideBiText.en": {"$regex": regex}}, {"sources.outsideBiText.he": {"$regex": regex}}]})

        sheets_list = []

        for sheet in sheets:
            sheets_list.append({"id": sheet["id"], "title": strip_tags(sheet["title"]), "owner": user_link(sheet["owner"])})

        return render_template(request, 'spam_dashboard.html', None, {
            "title": "Potential Spam Sheets since %s" % date.strftime("%Y-%m-%d"),
            "sheets": sheets_list,
            "type": "sheet",
        })
Esempio n. 4
0
def compare(request, comp_ref=None, lang=None, v1=None, v2=None):
    print(comp_ref)
    ref_array = None
    sec_ref = ""
    if comp_ref and Ref.is_ref(comp_ref):
        o_comp_ref = Ref(comp_ref)
        sec_ref = o_comp_ref.first_available_section_ref()
        if not sec_ref.is_section_level():
            sec_ref = sec_ref.section_ref()
        if o_comp_ref.is_book_level():
            o_comp_ref = sec_ref
        sec_ref = sec_ref.normal()
        if not o_comp_ref.is_section_level():
            ref_array = [r.normal() for r in o_comp_ref.all_subrefs()]
    if v1:
        v1 = v1.replace("_", " ")
    if v2:
        v2 = v2.replace("_", " ")

    return render_template(request,'compare.html', None, {
        "JSON_PROPS": json.dumps({
            'secRef': sec_ref,
            'v1': v1, 'v2': v2,
            'lang': lang,
            'refArray': ref_array,
        })
    })
Esempio n. 5
0
def view_visual_sheet(request, sheet_id):
    """
    View the sheet with sheet_id.
    """
    sheet = get_sheet(sheet_id)
    if "error" in sheet:
        return HttpResponse(sheet["error"])

    sheet["sources"] = annotate_user_links(sheet["sources"])

    # Count this as a view
    db.sheets.update({"id": int(sheet_id)}, {"$inc": {"views": 1}})

    try:
        owner = User.objects.get(id=sheet["owner"])
        author = owner.first_name + " " + owner.last_name
    except User.DoesNotExist:
        author = "Someone Mysterious"

    sheet_class     = make_sheet_class_string(sheet)
    can_edit_flag   = can_edit(request.user, sheet)
    can_add_flag    = can_add(request.user, sheet)

    return render_template(request,'sheets_visual.html', None, {
        "sheetJSON": json.dumps(sheet),
        "sheet": sheet,
        "sheet_class": sheet_class,
        "can_edit": can_edit_flag,
        "can_add": can_add_flag,
        "title": sheet["title"],
        "author": author,
        "is_owner": request.user.id == sheet["owner"],
        "is_public": sheet["status"] == "public",
        "current_url": request.get_full_path,
    })
Esempio n. 6
0
def view_sheet(request, sheet_id, editorMode = False):
    """
    View the sheet with sheet_id.
    """
    editor = request.GET.get('editor', '0')
    embed = request.GET.get('embed', '0')

    if editor != '1' and embed !='1' and editorMode is False:
        return catchall(request, sheet_id, True)

    sheet_id = int(sheet_id)
    sheet = get_sheet(sheet_id)
    if "error" in sheet and sheet["error"] != "Sheet updated.":
            return HttpResponse(sheet["error"])

    sheet["sources"] = annotate_user_links(sheet["sources"])

    # Count this as a view
    db.sheets.update({"id": sheet_id}, {"$inc": {"views": 1}})

    try:
        owner = User.objects.get(id=sheet["owner"])
        author = owner.first_name + " " + owner.last_name
    except User.DoesNotExist:
        author = "Someone Mysterious"

    sheet_class          = make_sheet_class_string(sheet)
    sheet_collections    = get_user_collections_for_sheet(request.user.id, sheet_id) if sheet["owner"] == request.user.id else None
    displayed_collection = Collection().load({"slug": sheet["displayedCollection"]}) if sheet.get("displayedCollection", None) else None
    embed_flag           = "embed" in request.GET
    likes                = sheet.get("likes", [])
    like_count           = len(likes)
    if request.user.is_authenticated:
        can_edit_flag    = can_edit(request.user, sheet)
        can_add_flag     = can_add(request.user, sheet)
        viewer_is_liker  = request.user.id in likes
    else:
        can_edit_flag    = False
        can_add_flag     = False
        viewer_is_liker  = False

    canonical_url = request.get_full_path().replace("?embed=1", "").replace("&embed=1", "")

    return render_template(request,'sheets.html', None, {
        "sheetJSON": json.dumps(sheet),
        "sheet": sheet,
        "sheet_class": sheet_class,
        "can_edit": can_edit_flag,
        "can_add": can_add_flag,
        "title": sheet["title"],
        "author": author,
        "is_owner": request.user.id == sheet["owner"],
        "sheet_collections": sheet_collections,
        "displayed_collection":  displayed_collection,
        "like_count": like_count,
        "viewer_is_liker": viewer_is_liker,
        "current_url": request.get_full_path,
        "canonical_url": canonical_url,
        "assignments_from_sheet":assignments_from_sheet(sheet_id),
    })
Esempio n. 7
0
def profile_spam_dashboard(request):

    from django.contrib.auth.models import User

    if request.method == 'POST':
        return jsonResponse({"error": "Unsupported Method: {}".format(request.method)})

    else:
        date = request.GET.get("date", None)

        if date:
            date = datetime.strptime(date, '%Y-%m-%d')

        else:
            date = request.GET.get("date", datetime.now() - timedelta(days=30))

        earliest_new_user_id = User.objects.filter(date_joined__gte=date).order_by('date_joined')[0].id

        regex = r'.*(?!href=[\'"](\/|http(s)?:\/\/(www\.)?sefaria).+[\'"])(href).*'

        spam_keywords_regex = r'(?i).*support.*|.*coin.*|.*helpline.*|.*base.*'

        users_to_check = db.profiles.find(
            {'$and': [
                {"id": {"$gt": earliest_new_user_id}, "reviewed": {"$ne": True}, "settings.reading_history": {"$ne": False}},
                {'$or': [
                    {'website': {"$ne": ""}},
                    {'facebook': {"$ne": ""}},
                    {'twitter': {"$ne": ""}},
                    {'youtube': {"$ne": ""}},
                    {'linkedin': {"$ne": ""}},
                    {'bio': {"$regex": regex}},
                    {'slug': {"$regex": spam_keywords_regex}}
            ]
        }]})



        profiles_list = []

        for user in users_to_check:
            history_count = db.user_history.find({'uid': user['id'], 'book': {'$ne': 'Sheet'}}).count()
            if history_count < 10:
                profile = model.user_profile.UserProfile(id=user["id"])

                profiles_list.append({"name": f"{profile.first_name} {profile.last_name}", "email": profile.email, "id": user["id"], "slug": user["slug"], "bio": strip_tags(user["bio"][0:250]), "website": user["website"][0:50]})

        return render_template(request, 'spam_dashboard.html', None, {
            "title": "Potential Spam Profiles since %s" % date.strftime("%Y-%m-%d"),
            "profiles": profiles_list,
            "type": "profile",
        })
Esempio n. 8
0
def new_sheet(request):
    profile = UserProfile(id=request.user.id)
    if getattr(profile, "uses_new_editor", False):
        sheet = {
            'status': 'unlisted',
            'title': '',
            'sources': [],
            'nextNode': 1,
            'options': {
                'layout': "stacked",
                'boxed': 0,
                'language': "bilingual",
                'numbered': 0,
                'assignable': 0,
                'divineNames': "noSub",
                'collaboration': "none",
                'highlightMode': 0,
                'langLayout': "heRight",
                'bsd': 0,
            }
        }

        responseSheet = save_sheet(sheet, request.user.id)
        return catchall(request, str(responseSheet["id"]), True)
    """
	View an new, empty source sheet.
	"""
    if "assignment" in request.GET:
        sheet_id = int(request.GET["assignment"])

        query = {"owner": request.user.id or -1, "assignment_id": sheet_id}
        existingAssignment = db.sheets.find_one(query) or []
        if "id" in existingAssignment:
            return view_sheet(request, str(existingAssignment["id"]), True)

        if "assignable" in db.sheets.find_one({"id": sheet_id})["options"]:
            if db.sheets.find_one({"id":
                                   sheet_id})["options"]["assignable"] == 1:
                return assigned_sheet(request, sheet_id)

    query = {"owner": request.user.id or -1}
    hide_video = db.sheets.count_documents(query) > 2

    return render_template(
        request, 'sheets.html', None, {
            "can_edit": True,
            "new_sheet": True,
            "is_owner": True,
            "hide_video": hide_video,
            "current_url": request.get_full_path,
        })
Esempio n. 9
0
def compare(request, secRef=None, lang=None, v1=None, v2=None):
    if secRef and Ref.is_ref(secRef):
        secRef = Ref(secRef).first_available_section_ref()
        if not secRef.is_section_level():
            secRef = secRef.section_ref()
        secRef = secRef.normal()
    if v1:
        v1 = v1.replace("_", " ")
    if v2:
        v2 = v2.replace("_", " ")

    return render_template(request,'compare.html', None, {
        "JSON_PROPS": json.dumps({
            'secRef': secRef,
            'v1': v1, 'v2': v2,
            'lang': lang,
        })
    })
Esempio n. 10
0
def assigned_sheet(request, assignment_id):
    """
    A new sheet prefilled with an assignment.
    """
    sheet = get_sheet(assignment_id)
    if "error" in sheet:
        return HttpResponse(sheet["error"])

    sheet["sources"] = annotate_user_links(sheet["sources"])

    # Remove keys from we don't want transferred
    for key in ("id", "like", "views", "displayedCollection"):
        if key in sheet:
            del sheet[key]

    assigner = UserProfile(id=sheet["owner"])
    assigner_id = assigner.id
    sheet_class = make_sheet_class_string(sheet)
    can_edit_flag = True
    can_add_flag = can_add(request.user, sheet)
    embed_flag = "embed" in request.GET
    likes = sheet.get("likes", [])
    like_count = len(likes)
    viewer_is_liker = request.user.id in likes

    return render_template(
        request, 'sheets.html', None, {
            "sheetJSON": json.dumps(sheet),
            "sheet": sheet,
            "assignment_id": assignment_id,
            "assigner_id": assigner_id,
            "new_sheet": True,
            "sheet_class": sheet_class,
            "can_edit": can_edit_flag,
            "can_add": can_add_flag,
            "title": sheet["title"],
            "is_owner": True,
            "is_public": sheet["status"] == "public",
            "sheet_collections": [],
            "displayed_collection": None,
            "like_count": like_count,
            "viewer_is_liker": viewer_is_liker,
            "current_url": request.get_full_path,
        })
Esempio n. 11
0
def profile_spam_dashboard(request):

    from django.contrib.auth.models import User

    if request.method == 'POST':
        return jsonResponse({"error": "Unsupported Method: {}".format(request.method)})

    else:
        date = request.GET.get("date", None)

        if date:
            date = datetime.strptime(date, '%Y-%m-%d')

        else:
            date = request.GET.get("date", datetime.now() - timedelta(days=30))

        earliest_new_user_id = User.objects.filter(date_joined__gte=date).order_by('date_joined')[0].id

        regex = r'.*(?!href=[\'"](\/|http(s)?:\/\/(www\.)?sefaria).+[\'"])(href).*'

        users_to_check = db.profiles.find(
            {'$or': [
                {'website': {"$ne": ""}, 'bio': {"$ne": ""}, "id": {"$gt": earliest_new_user_id},
                      "reviewed": {"$ne": True}},
                {'bio': {"$regex": regex}, "id": {"$gt": earliest_new_user_id}, "reviewed": {"$ne": True}}
            ]
        })



        profiles_list = []

        for user in users_to_check:
            history_count = db.user_history.find({'uid': user['id']}).count()
            if history_count < 10:
                profiles_list.append({"id": user["id"], "slug": user["slug"], "bio": strip_tags(user["bio"][0:250]), "website": user["website"][0:50]})

        return render_template(request, 'spam_dashboard.html', None, {
            "title": "Potential Spam Profiles since %s" % date.strftime("%Y-%m-%d"),
            "profiles": profiles_list,
            "type": "profile",
        })
Esempio n. 12
0
def accounts(request):
    return render_template(request,"registration/accounts.html", None, {
        "createForm": UserCreationForm(),
        "loginForm": AuthenticationForm()
    })
Esempio n. 13
0
def maintenance_message(request):
    resp = render_template(request,"static/maintenance.html", None, {"message": MAINTENANCE_MESSAGE}, status=503)
    return resp
Esempio n. 14
0
def spam_dashboard(request):
    from django.contrib.auth.models import User


    def purge_spammer_account_data(spammer_id):
        # Delete Sheets
        db.sheets.delete_many({"owner": spammer_id})
        # Delete Notes
        db.notes.delete_many({"owner": spammer_id})
        # Delete Notifcations
        db.notifications.delete_many({"uid": spammer_id})
        # Delete Following Relationships
        db.following.delete_many({"follower": spammer_id})
        db.following.delete_many({"followee": spammer_id})
        # Delete Profile
        db.profiles.delete_one({"id": spammer_id})

        # Set account inactive
        spammer_account = User.objects.get(id=spammer_id)
        spammer_account.is_active = False
        spammer_account.save()


    if request.method == 'POST':
        req_type = request.POST.get("type")

        if req_type == "sheet":
            spam_sheet_ids = list(map(int, request.POST.getlist("spam_sheets[]", [])))
            reviewed_sheet_ids = list(map(int, request.POST.getlist("reviewed_sheets[]", [])))
            db.sheets.update_many({"id": {"$in": reviewed_sheet_ids}}, {"$set": {"reviewed": True}})
            spammers = db.sheets.find({"id": {"$in": spam_sheet_ids}}, {"owner": 1}).distinct("owner")
            db.sheets.delete_many({"id": {"$in": spam_sheet_ids}})

            for spammer in spammers:
                try:
                    purge_spammer_account_data(spammer)
                except:
                    continue

            return render_template(request, 'spam_dashboard.html', None, {
                "deleted": len(spam_sheet_ids),
                "ids": spam_sheet_ids,
                "reviewed": len(reviewed_sheet_ids),
                "spammers_deactivated": len(spammers)
            })

        elif req_type == "profile":
            spam_profile_ids = list(map(int, request.POST.getlist("spam_profiles[]", [])))
            reviewed_profile_ids = list(map(int, request.POST.getlist("reviewed_profiles[]", [])))
            db.profiles.update_many({"id": {"$in": reviewed_profile_ids}}, {"$set": {"reviewed": True}})

            for spammer in spam_profile_ids:
                try:
                    purge_spammer_account_data(spammer)
                except:
                    continue

            return render_template(request, 'spam_dashboard.html', None, {
                "deleted": len(spam_profile_ids),
                "ids": spam_profile_ids,
                "reviewed": len(reviewed_profile_ids),
                "spammers_deactivated": len(spam_profile_ids)
            })

        else:
            return jsonResponse({"error": "Unknown post type."})

    else:
        return jsonResponse({"error": "Unsupported Method: {}".format(request.method)})