Skip to content

Commit

Permalink
things starting to come together
Browse files Browse the repository at this point in the history
  • Loading branch information
daxaxelrod committed Nov 1, 2023
1 parent 713ffc8 commit d97dcd7
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 50 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ scipy = "*"
scrapy = "*"
linkedin-scraper = "*"
selenium = "*"
requests = "*"

[dev-packages]
black = "*"
Expand Down
8 changes: 3 additions & 5 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 36 additions & 6 deletions frontend/src/app/components/users/profile/UserOpenInsureRating.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import React, { useMemo } from "react";
import { Col, Flex, Progress, Row } from "antd";
import { Col, Flex, Progress, Row, Spin } from "antd";
import { User } from "../../../../redux/reducers/types/commonTypes";
import colors from "../../../constants/colors";
import { Typography } from "antd";
import { ReloadOutlined } from "@ant-design/icons";
import { useAppDispatch, useAppSelector } from "../../../../redux/hooks";
import { getUserRepuation } from "../../../../redux/actions/users";

const { Title, Paragraph } = Typography;

export default function UserOpenInsureRating({ user }: { user: User }) {
const { reputation } = user;
const { total_score } = reputation || {};
const dispatch = useAppDispatch();
const getReputationPending = useAppSelector(
(state) => state.users.getUserReputationPending
);

const hasScore = !!total_score;

Expand All @@ -30,6 +36,10 @@ export default function UserOpenInsureRating({ user }: { user: User }) {
return "No score yet";
}, [total_score]);

const getReputation = () => {
dispatch(getUserRepuation(user.id));
};

return (
<>
<Flex>
Expand All @@ -39,13 +49,33 @@ export default function UserOpenInsureRating({ user }: { user: User }) {
top: 0,
right: 0,
zIndex: 1,
display: "flex",
}}
>
<ReloadOutlined
style={{ marginRight: 4, color: colors.gray7 }}
/>
<Paragraph style={{ marginBottom: 0 }}>Refresh</Paragraph>
<div
onClick={getReputation}
style={{
cursor: "pointer",
display: "flex",
alignItems: "center",
justifyContent: "center",
flexDirection: "row",
}}
>
{getReputationPending ? (
<Spin />
) : (
<ReloadOutlined style={{ color: colors.gray7 }} />
)}
<Paragraph
style={{
marginLeft: 6,
marginBottom: 0,
color: colors.gray9,
}}
>
Refresh
</Paragraph>
</div>
</div>
<Progress
type="dashboard"
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/networking/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export function joinMailingList(email: string) {
export function getUserDetail(userId: number) {
return axiosInstance.get(`/api/v1/users/${userId}/`);
}

export function getUserReputation(userId: number) {
return axiosInstance.get(`/api/v1/users/${userId}/reputation/`);
}
3 changes: 3 additions & 0 deletions frontend/src/redux/actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ export const SET_POLICY_DETAIL_TAB_KEY = "ui/SET_POLICY_DETAIL_TAB_KEY";
export const GET_USER_DETAIL_PENDING = "user/GET_USER_DETAIL_PENDING";
export const GET_USER_DETAIL_SUCCESS = "user/GET_USER_DETAIL_SUCCESS";
export const GET_USER_DETAIL_FAILURE = "user/GET_USER_DETAIL_FAILURE";
export const GET_USER_REPUTATION_PENDING = "user/GET_USER_REPUTATION_PENDING";
export const GET_USER_REPUTATION_SUCCESS = "user/GET_USER_REPUTATION_SUCCESS";
export const GET_USER_REPUTATION_FAILURE = "user/GET_USER_REPUTATION_FAILURE";

// guesses
export const GET_AVAILABLE_POLICIES_LINES_PENDING =
Expand Down
22 changes: 22 additions & 0 deletions frontend/src/redux/actions/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
GET_USER_DETAIL_PENDING,
GET_USER_DETAIL_SUCCESS,
GET_USER_DETAIL_FAILURE,
GET_USER_REPUTATION_PENDING,
GET_USER_REPUTATION_SUCCESS,
GET_USER_REPUTATION_FAILURE,
} from "./types";
import * as API from "../../networking/users";

Expand All @@ -27,3 +30,22 @@ export const getDetailedUserProfile =
return null;
}
};

export const getUserRepuation =
(userId: number): ThunkAction<void, RootState, unknown, AnyAction> =>
async (dispatch) => {
dispatch({ type: GET_USER_REPUTATION_PENDING });
try {
const response = await API.getUserReputation(userId);
debugger;
dispatch({
type: GET_USER_REPUTATION_SUCCESS,
payload: {
userId,
reputation: response.data,
},
});
} catch (error) {
dispatch({ type: GET_USER_REPUTATION_FAILURE, payload: error });
}
};
27 changes: 27 additions & 0 deletions frontend/src/redux/reducers/usersReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import {
GET_USER_DETAIL_PENDING,
GET_USER_DETAIL_SUCCESS,
GET_USER_DETAIL_FAILURE,
GET_USER_REPUTATION_PENDING,
GET_USER_REPUTATION_SUCCESS,
GET_USER_REPUTATION_FAILURE,
} from "../actions/types";
import { User, Policy } from "./types/commonTypes";

export interface UIState {
getUserDetailPending: boolean;
users: Record<number, User>;
getUserReputationPending: boolean;
}

const initialState: UIState = {
users: {},
getUserDetailPending: false,
getUserReputationPending: false,
};

export default (state = initialState, { type, payload }: AnyAction) => {
Expand Down Expand Up @@ -53,6 +58,28 @@ export default (state = initialState, { type, payload }: AnyAction) => {
...state,
getUserDetailPending: false,
};
case GET_USER_REPUTATION_PENDING:
return {
...state,
getUserReputationPending: true,
};
case GET_USER_REPUTATION_SUCCESS:
return {
...state,
getUserReputationPending: false,
users: {
...state.users,
[payload.userId]: {
...state.users[payload.userId],
reputation: payload.reputation,
},
},
};
case GET_USER_REPUTATION_FAILURE:
return {
...state,
getUserReputationPending: false,
};

default:
return state;
Expand Down
3 changes: 2 additions & 1 deletion pods/reputation/background.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pods.models import User
from linkedin_scraper import Person


def get_reputation_from_background(user: User, linkedin_profile):
def get_reputation_from_background(user: User, linkedin_profile: Person):
return 100
3 changes: 2 additions & 1 deletion pods/reputation/lifestyle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pods.models import User
from linkedin_scraper import Person


def get_reputation_from_lifestyle(user: User, linkedin_profile):
def get_reputation_from_lifestyle(user: User, linkedin_profile: Person):
return 100
76 changes: 39 additions & 37 deletions pods/reputation/reputation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,50 @@

linkedin_email = settings.LINKEDIN_EMAIL
linkedin_password = settings.LINKEDIN_PASSWORD
driver = webdriver.Chrome()


def scrape_linkedin_profile(linked_url):
if not linked_url:
def scrape_linkedin_profile(linkedin_url) -> Person:
if not linkedin_url:
return {}
driver = webdriver.Chrome()
# login action prevents security check
actions.login(driver, linkedin_email, linkedin_password)
person = Person(linked_url, driver=driver)
import pdb

pdb.set_trace()
profile = {}
return profile
person = Person(linkedin_url, driver=driver)
return person


def determine_reputation_for_user(user):
last_reputation: ReputationDetails = user.reputation_results.latest("created_at")
now = timezone.now()
if now < last_reputation.next_refresh_available:
return last_reputation
else:
linkedin_profile = scrape_linkedin_profile(user.linked_url)

payments = get_reputation_from_payments(user)
claims = get_reputation_from_claims(user)
background = get_reputation_from_background(user, linkedin_profile)
activity = get_reputation_from_activity(user)
lifestyle = get_reputation_from_lifestyle(user, linkedin_profile)

total_score = (payments + claims + background + activity + lifestyle) / 5

reputation = ReputationDetails.objects.create(
user=user,
calculated_on=timezone.now(),
next_refresh_available=now
+ timezone.timedelta(days=settings.REPUTATION_REFRESH_COOLDAY_DAYS),
total_score=total_score,
payments=payments,
claims=claims,
background=background,
activity=activity,
lifestyle=lifestyle,
try:
last_reputation: ReputationDetails = user.reputation_results.latest(
"created_at"
)

return reputation
now = timezone.now()
if last_reputation and now < last_reputation.next_refresh_available:
return last_reputation
except ReputationDetails.DoesNotExist:
pass

linkedin_profile = scrape_linkedin_profile(user.linkedin_url)

payments = get_reputation_from_payments(user)
claims = get_reputation_from_claims(user)
background = get_reputation_from_background(user, linkedin_profile)
activity = get_reputation_from_activity(user)
lifestyle = get_reputation_from_lifestyle(user, linkedin_profile)

total_score = (payments + claims + background + activity + lifestyle) / 5

reputation = ReputationDetails.objects.create(
user=user,
calculated_on=timezone.now(),
next_refresh_available=now
+ timezone.timedelta(days=settings.REPUTATION_REFRESH_COOLDAY_DAYS),
total_score=total_score,
payments=payments,
claims=claims,
background=background,
activity=activity,
lifestyle=lifestyle,
)

return reputation

0 comments on commit d97dcd7

Please sign in to comment.